function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ms-hase-q23eems-hase-q23ee 

ユーザーチェックを行うAPIは?

お世話になっております、アプリの中で、ユーザ認証を2度行いたいのですが、どのAPIを使用すればよいか分かる方はいらっしゃいますか?
現在はUserオブジェクトからユーザー名の存在チェックのみ行っているのですが、下のようなコードが書ければ、と考えています

宜しくお願いします

public login(String userId, String passwd) {
    int result = xxxx.login(userId,pass);
    if (result ==  VALID_USER) {
        debug('このユーザーは正常です');
    } else {
        debug('入力されたユーザー名またはパスワードが間違っています');
    }
}



 
Shingo YamazakiShingo Yamazaki
ms-hase-q23eeさん

こんにちは。
山﨑と申します。

実際に開発経験があるわけではありませんが、
ユーザ名とパスワードによる認証はこちらが参考になるかもしれません。

http://www.salesforcegeneral.com/salesforce-articles/salesforce-re-enter-passwords-to-verify-users-login.html

Apex コードをこちらにも転記します。
doVerify() を少し変更して、username と password を受け取る形にすれば良いかと。
(ご提示いただいたサンプルコードですと userId を引数に取っているので、ご期待に沿えなかったらすみません)
public class VerifyPassword {
 
    public final String LOGIN_DOMAIN = 'test'; //other options: test, prerellogin.pre
 
    public String username {get{ return UserInfo.getUsername(); }}
    public transient String password {get;set;}
 
    public PageReference doVerify(){
         
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://' + LOGIN_DOMAIN + '.salesforce.com/services/Soap/u/22.0');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody(buildSoapLogin(username,password));
 
        //basically if there is a loginResponse element, then login succeeded; else there
        //  would be soap fault element after body
        final Boolean verified = (new Http()).send(request).getBodyDocument().getRootElement()
          .getChildElement('Body','http://schemas.xmlsoap.org/soap/envelope/')
          .getChildElement('loginResponse','urn:partner.soap.sforce.com') != null;
 
        if(verified) ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Correct password!'));
        else         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Incorrect password!'));
 
        return null;
    }
 
    public static String buildSoapLogin(String username, String password){
        XmlStreamWriter w = new XmlStreamWriter();
        w.writeStartElement('', 'login', 'urn:partner.soap.sforce.com');
        w.writeNamespace('', 'urn:partner.soap.sforce.com');
        w.writeStartElement('', 'username', 'urn:partner.soap.sforce.com');
        w.writeCharacters(username);
        w.writeEndElement();
        w.writeStartElement('', 'password', 'urn:partner.soap.sforce.com');
        w.writeCharacters(password);
        w.writeEndElement();
        w.writeEndElement();
         
        String xmlOutput =
              '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body>'
            + w.getXmlString()
            + '</Body></Envelope>';
        w.close();
        return xmlOutput;
    }
     
}
ms-hase-q23eems-hase-q23ee
回答ありがとうございます。ですが、私がやりたいのはコミュニティユーザー(外部ユーザー)なので、".salesforce.com"による認証は使用できません。調べてみた所、site.login()関数が使えそうなのですが、戻り値がPageReferenceなのが難点です。中に何が入っているのかわからない・・・

上記を変更してこんな感じで書きたいのですが、※にあたる部分を知っている方は居ますでしょうか?

public login(String userId, String passwd) {
    PageReference result = Site.login(userId,pass);
    if (このページリファレンスが正常なログイン後のページを示している == true) {  // ※
        debug('このユーザーは正常です');
    } else {
        debug('入力されたユーザー名またはパスワードが間違っています');
    }
}


 
Shingo YamazakiShingo Yamazaki
なるほど、コミュニティユーザーの方でしたか。
Site.login() ですとこのあたりを参考にされてはいかがでしょうか。

http://blogbloggerbloggesta.blog.fc2.com/blog-entry-30.html
http://salesforce.stackexchange.com/questions/19364/authenticate-users-on-salesforce-communities

また、Site クラスのリファレンスは
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_sites.htm#apex_System_Site_login

このあたりを見ていると、Site.login() は
・ログインに成功すると第3引数に指定した URL にリダイレクトするための PageReference オブジェクトを返す
・ログインに失敗すると null を返す
ので、他の方もされているように
 
PageReference pr = Site.login(username, password, '/FirstPageAfterLogin');
if (pr != null && pr.getUrl() == '/FirstPageAfterLogin') {  // この行が※に相当
    // 認証成功
}

でどうでしょうか。
ms-hase-q23eems-hase-q23ee
site.login()関数は呼出コンテキストか何かに制限があるのでしょうか?REST APIから呼び出しているのですが、何を渡してもPageReferenceがnullしか戻しません。理由がまったくわからない
Shingo YamazakiShingo Yamazaki
リファレンス等にはそういった記述は見られませんね。
(ただ、リファレンスには書いていないが...ということは多々あります。。。)

「REST API から呼び出している」というのは、@RestResource アノテーションを使っているということでしょうか?
また、直接実行した場合は正常に動くことは確認済みでしょうか?