アプリ管理者向け機能
JavaScript SDK は、全てのメソッドを管理者権限で実行する管理者機能を提供します。自らが管理するサーバー上で JavaScript SDK を利用する場合などにおいて活用してみてください。
管理者機能を利用する場合は、まず次のように KiiAppAdminContext
のコンテキストを作成してください。Server Code で context.getAppAdminContext()
を使って管理者トークンを取得した場合は、すでに adminContext
が使える状態にあるため、作成の操作は不要です。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { // adminContext : KiiAppAdminContext instance // Operate entities with adminContext. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { // adminContext : KiiAppAdminContext instance // Operate entities with adminContext. }, failure: function(errorString, statusCode) { // Handle the error. } });
authenticateAsAppAdmin
メソッド実行時に、開発者ポータルでアプリを作成した際に払いだされた ClientID
と ClientSecret
を指定してください。ClientID と ClientSecret を確認する方法は アクセスキーの確認とリセット をご参照ください。
注意: ClientID と ClientSecret は、第三者に決して知られてはならない機密情報です。第三者が閲覧可能なコード内に ClientID や ClientSecret は絶対に記載しないでください。管理者権限での API 実行は、自らが管理するサーバー内での処理等、コード内容が非公開な状況下において利用されることを想定しています。
ClientID と ClientSecret 漏洩に伴うリスクについては 管理者用のアクセスキーの漏洩とリスク をご確認ください。
管理者権限での実行
作成した KiiAppAdminContext
インスタンスのメソッドを使うと、管理者権限での操作ができます。この際、操作の対象となる KiiObject
や KiiUser
などのインスタンスは、KiiAppAdminContext
インスタンスから直接的または間接的に作成されている必要があります。
図の左側のように、
KiiAppAdminContext
インスタンスから操作の対象となるKiiObject
やKiiUser
などのインスタンスを作成し、それらのメソッドを使って Kii Cloud にアクセスすると、管理者権限で処理が実行されます。図の右側のように、
Kii.bucketWithName()
メソッドなどを使って操作対象のインスタンスを直接生成すると、ログイン中のユーザー(またはログイン前の匿名ユーザー)の権限で処理が実行されます。
管理者権限でのアクセスは、authenticateAsAppAdmin()
メソッドの実行後かどうかではなく、KiiAppAdminContext
インスタンスから操作対象のインスタンスを生成しているかどうかによって決まる点にご注意ください。
管理者権限で KiiUser を操作する
次のように userWithID
メソッドを実行して KiiUser
インスタンスを作成します。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var user = adminContext.userWithID("put existing user id here"); // KiiUser operation by app admin is available now. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var user = adminContext.userWithID("put existing user id here"); // KiiUser operation by app admin is available now. }, failure: function(errorString, statusCode) { // Handle the error. } });
以後、該当ユーザーに対するフルアクセスが可能になります。
管理者権限で KiiGroup を操作する
新規に KiiGroup を作成し操作する
次のように groupWithName
メソッドを実行して KiiGroup
インスタンスを作成し、saveWithOwner
メソッドでオーナーのユーザー ID を指定して KiiGroup を保存します。通常の save
で保存した場合、オーナーが存在しない KiiGroup が作成されるので注意してください。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var group = adminContext.groupWithName("new_group_name"); return group.saveWithOwner("Owner UserID"); } ).then( function(theGroup) { // KiiGroup operation by app admin is available now. } ).catch( function(error) { var theGroup = error.target; // for saveWithOwner() var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var group = adminContext.groupWithName("new_group_name"); group.saveWithOwner("Owner UserID", { success: function(savedGroup) { // KiiGroup operation by app admin is available now. }, failure: function(theGroup, errorString) { // Handle the error. } }); }, failure: function(errorString, errorCode) { // Handle the error. } });
ID を指定して新規に KiiGroup を作成し操作する
次のように registerGroupWithOwnerAndID
メソッドを実行すると、グループの ID を指定して KiiGroup を作成することもできます。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var ownerId = "owner's user id"; var groupId = "my-group-1"; var groupName = "myGroup"; var members = []; members.push(KiiUser.userWithID("member's user id")); return adminContext.registerGroupWithOwnerAndID(groupId, groupName, ownerId, members); } ).then( function(theGroup) { // KiiGroup operation by app admin is available now. } ).catch( function(error) { var theGroup = error.target; // for registerGroupWithOwnerAndID() var addMembersArray = error.addMembersArray; // for registerGroupWithOwnerAndID() var anErrorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var ownerId = "owner's user id"; var groupId = "my-group-1"; var groupName = "myGroup"; var members = []; members.push(KiiUser.userWithID("member's user id")); adminContext.registerGroupWithOwnerAndID(groupId, groupName, ownerId, members, { success: function(theGroup) { // KiiGroup operation by app admin is available now. }, failure: function(theGroup, errorString, addMembersArray) { // Handle the error. } }); }, failure: function(errorString, errorCode) { // Handle the error. } });
既存の KiiGroup を操作する
次のように groupWithURI
メソッドを実行して KiiGroup
インスタンスを作成します。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var group = adminContext.groupWithURI("put existing group uri here"); // KiiGroup operation by app admin is available now. // Need to execute refresh() before accessing the group. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var group = adminContext.groupWithURI("put existing group uri here"); // KiiGroup operation by app admin is available now. // Need to execute refresh() before accessing the group. }, failure: function(errorString, errorCode) { // Handle the error. } });
また、次のように groupWithID
メソッドを実行すると、既存のグループの KiiGroup
インスタンスが作成できます。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var group = adminContext.groupWithID("put existing group ID here"); // KiiGroup operation by app admin is available now. // Need to execute refresh() before accessing the group. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var group = adminContext.groupWithID("put existing group ID here"); // KiiGroup operation by app admin is available now. // Need to execute refresh() before accessing the group. }, failure: function(errorString, errorCode) { // Handle the error. } });
いずれのケースにおいても、以後該当グループに対するフルアクセスが可能になります。
管理者権限で KiiBucket を操作する
次のように bucketWithName
メソッドを実行してアプリケーションスコープに KiiBucket
インスタンスを作成します。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var bucket = adminContext.bucketWithName("bucket_name"); // KiiBucket operation by app admin is available now. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var bucket = adminContext.bucketWithName("bucket_name"); // KiiBucket operation by app admin is available now. }, failure: function(errorString, errorCode) { // Handle the error. } });
- 新規 Bucket を作成した場合は、アプリケーションスコープの Bucket になります。
- 既存 Bucket をインスタンス化した場合は、この Bucket に対して管理者権限でアクセスできます。
管理者権限で KiiObject を操作する
次のように objectWithURI
メソッドを実行して KiiObject
インスタンスを作成します。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var object = adminContext.objectWithURI("Set the URI of an existing KiiObject here"); // KiiObject operation by app admin is available now. // Need to execute refresh() before accessing the object. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var object = adminContext.objectWithURI("Set the URI of an existing KiiObject here"); // KiiObject operation by app admin is available now. // Need to execute refresh() before accessing the object. }, failure: function(errorString, errorCode) { // Handle the error. } });
- 新規 Object を作成した場合は、アプリケーションスコープの Object になります。
- 既存 Object をインスタンス化した場合は、この Object に対して管理者権限でアクセスできます。
管理者権限で KiiUser を検索する
次のように findUserByUsername
メソッドを実行して KiiUser
を検索します。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { return adminContext.findUserByUsername("user_name_to_find"); } ).then( function(params) { var adminContext = params[0]; var foundUser = params[1]; // Do something. } ).catch( function(error) { var adminContext = error.target; // for findUserByUsername() var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { adminContext.findUserByUsername("user_name_to_find", { success: function(adminContext, foundUser) { // KiiUser operation by app admin is available now. }, failure: function(adminContext, errorString) { // Handle the error. } }); }, failure: function(errorString, errorCode) { // Handle the error. } });
以後、検索したユーザーに対するフルアクセスが可能になります。
なお検索したユーザーの情報は、"Expose Full User Data to Others" オプションの設定にかかわらず、ユーザーの全情報が返されます。
他にも findUserByEmail
(メールアドレス)や findUserByPhone
(電話番号)を利用して KiiUser を検索できます。
管理者権限で KiiTopic を操作する
次のように topicWithName
メソッドを実行してアプリケーションスコープの KiiTopic
インスタンスを取得します。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { var topic = adminContext.topicWithName("topic_name"); // KiiTopic operation by app admin is available now. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { var topic = adminContext.topicWithName("topic_name"); // KiiTopic operation by app admin is available now. }, failure: function(errorString, statusCode) { // Handle the error. } });
この機能の利用例は以下をご覧ください。
管理者権限で KiiThing を操作する
次のように loadThingWithThingID
メソッドを実行して KiiThing
インスタンスを取得します。
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { return adminContext.loadThingWithThingID("put existing thing ID here"); } ).then( function(thing) { // KiiThing operation by app admin is available now. } ).catch( function(error) { var errorString = error.message; // Handle the error. } );
-
Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { adminContext.loadThingWithThingID("put existing thing ID here", { success: function(thing) { // KiiThing operation by app admin is available now. }, failure: function(error) { // Handle the error. } }); }, failure: function(errorString, errorCode) { // Handle the error. } });
このコードは thingID を指定して KiiThing を取得する例ですが、loadThingWithVendorThingID
メソッドでは vendorThingID 指定での取得が、registerThing
では新規の Thing の登録が可能です。それぞれ、JSDoc をご覧ください。
KiiThing を使った処理の詳細は クライアント SDK から Thing を利用 以下のページをご覧ください。