LoginSignup
7
2

More than 3 years have passed since last update.

Googleチャットから、Salesforceアカウントロック解除を

Last updated at Posted at 2019-12-06

はじめに

Salesforceのアカウントがロックされると、システム管理者がロック解除をしない限り、
一般ユーザのログインができなくなってしまいます。孤独なシステム管理者にとって、
24時間体制でロック解除を行うのは、あまりに不憫です。
せめて時間外に自動でロック解除できる仕組みができないかと思い、Googleチャットから
解除申請を行う仕組みを考えてみました。

WS000143_4.JPG

手順1:ロック解除申請の流れ

 ①解除申請できるのは自分のアカウントのみとする
 ②解除申請の第一段階として、申請者のGmailに確認コードを送信し、チャット上での入力を促す
 ③確認コードの確認後、申請者のロック解除を行う
 ④ロック解除メールがGmailに送信される

手順2:Salesforce側の申請受付~ロック解除の構築(Apex)

  • チャットからの解除申請を受け入れるAPIをApexクラスに構築します。
  1. 解除申請を受けて、確認コードを送信する処理
UserUnlocl.cls
/**
 * 認証コード送信処理
*/
@HttpPut
global static ResponseWrapper doSendCode(){
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    ResponseWrapper response = new ResponseWrapper();
    response.isSuccess = false;
    // パラメータ取得(ユーザー名)
    String userName = req.params.get('userName');
    try{
        // ユーザー情報取得
        User us = [SELECT id,Email FROM user WHERE Username =:userName];
        if(us != null){
            // 認証コード生成(5桁)テキスト
            String CustomVerificationCode = String.valueof(Math.round(Math.random()*100000));
            us.CustomVerificationCode__c = CustomVerificationCode;
            update us;

            // メール送信
            String tmplateName = 'UserUnlockSendCodeEmailTemplate';
            EmailTemplate tempMail = [SELECT Id, Subject, Body FROM EmailTemplate WHERE DeveloperName = :tmplateName];
            String[] replaceValue = new String[2];
            replaceValue[0] = userName;
            replaceValue[1] = CustomVerificationCode;
            String resultBody = String.format(tempMail.body, replaceValue);
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            // 宛先設定
            mail.setToAddresses(new List<String>{us.Email});
            // 件名設定(テンプレートから取得)
            mail.setSubject(tempMail.Subject);
            // メール本文設定
            mail.setPlainTextBody(resultBody);
            // 送信
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            // 正常終了
            response.isSuccess = true;
        }
    } catch(Exception e){
        // 異常終了
        response.errorMessage = 'システムエラーが発生しました。' + e.getMessage();
        system.debug(e.getMessage());
    }
    return response;
}

2.確認コードの検証を行い、ロック解除を行う処理

UserUnlocl.cls
/**
* RESET API - UserUnlock & Send reset Email
*/
@HttpPatch
global static ResponseWrapper updateResetPassword(){
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    ResponseWrapper response = new ResponseWrapper();
    response.isSuccess = false;
    // パラメータ取得(ユーザー名)
    String userName = req.params.get('userName');
    // パラメータ取得(認証コード)
    String code = req.params.get('code');
    try{
        // ユーザー情報取得
        User us = [SELECT id FROM user WHERE Username =:userName AND CustomVerificationCode__c =:code];
        if(us != null){
                // ロックユーザー取得
                UserLogin ul = [SELECT id,IsPasswordLocked FROM UserLogin WHERE UserId =:us.id];
                // ロック解除
                ul.IsPasswordLocked = false;
                update ul;
                // ロック解除、パスワードリセット通知
                System.resetPassword(us.Id, true);
                // 正常終了
                response.isSuccess = true;
            }
    } catch(Exception e){
            // 異常終了
            response.errorMessage = 'システムエラー' + e.getMessage();
            system.debug(e.getMessage());
        }
    return response;
}

手順3:Googleチャット側に解除申請処理を構築(GAS)

  • チャットが呼び出す、解除申請処理をGASで構築します

公開されたAPexのAPIをGASから呼び出すようにスクリプトを作成して、完了です。

最後に

  • 簡単な設定で、システム管理者を介さずにロック解除の仕組みを構築できたかと思います。孤独で忙しいシステム管理者の工数削減につながれば幸いです。
7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2