Kelly は Finance 部門を管理しています。Kelly がユーザーを作成すると、そのユーザーは Finance 部門のメンバーとして割り当てられます。
function(ctx, callback) { var newProfile = { email: ctx.payload.email, password: ctx.payload.password, connection: ctx.payload.connection, user_metadata: ctx.payload.user_metadata, app_metadata: { department: ctx.payload.memberships && ctx.payload.memberships[0], ...ctx.payload.app_metadata } }; if (!ctx.payload.memberships || ctx.payload.memberships.length === 0) { return callback(new Error('The user must be created within a department.')); } // 現在のユーザーのメタデータから部署を取得します。 var currentDepartment = ctx.request.user.app_metadata && ctx.request.user.app_metadata.department; if (!currentDepartment || !currentDepartment.length) { return callback(new Error('The current user is not part of any department.')); } // IT部署に所属していない場合、自分の部署内でのみユーザーを作成できます。 // ITはすべての部署でユーザーを作成できます。 if (currentDepartment !== 'IT' && ctx.payload.memberships[0] !== currentDepartment) { return callback(new Error('You can only create users within your own department.')); } if (ctx.method === 'update') { // 更新の場合、送信が必要なフィールドのみを設定します Object.keys(newProfile).forEach(function(key) { if (newProfile[key] === ctx.request.originalUser[key]) delete newProfile[key]; }); } // これがAPI v2に送信されるペイロードです。API v2でのユーザー作成方法を完全に制御できます。 return callback(null, newProfile);}