Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public without sharing class MyClass {
- public String username{get;set;}
- public String password{get;set;}
- public PageReference logado {get;set;}
- public pageReference myMethod(){
- logado = Site.login(username, password, null);
- if(logado != null){
- // code
- }
- }
- }
- /**
- * An Apex class that creates a portal user
- */
- public class SiteRegisterController {
- // PORTAL_ACCOUNT_ID is the account on which the contact will be created on
- // and then enabled as a portal user.
- //Enter the account ID in place of <portal_account_id> below.
- private static Id PORTAL_ACCOUNT_ID = '<portal_account_id>';
- public SiteRegisterController () {
- }
- public String username {get; set;}
- public String email {get; set;}
- public String password {get; set {password = value == null ? value : value.trim(); } }
- public String confirmPassword {get; set { confirmPassword =
- value == null ? value : value.trim(); } }
- public String communityNickname {get; set { communityNickname =
- value == null ? value : value.trim(); } }
- private boolean isValidPassword() {
- return password == confirmPassword;
- }
- public PageReference registerUser() {
- // If password is null, a random password is sent to the user
- if (!isValidPassword()) {
- ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,
- Label.site.passwords_dont_match);
- ApexPages.addMessage(msg);
- return null;
- }
- User u = new User();
- u.Username = username;
- u.Email = email;
- u.CommunityNickname = communityNickname;
- String accountId = PORTAL_ACCOUNT_ID;
- // lastName is a required field on user, but if it isn't specified,
- the code uses the username
- String userId = Site.createPortalUser(u, accountId, password);
- if (userId != null) {
- if (password != null && password.length() > 1) {
- return Site.login(username, password, null);
- }
- else {
- PageReference page = System.Page.SiteRegisterConfirm;
- page.setRedirect(true);
- return page;
- }
- }
- return null;
- }
- }
- /**
- * Test class.
- */
- @isTest
- private class SiteRegisterControllerTest {
- // Test method for verifying the positive test case
- static testMethod void testRegistration() {
- SiteRegisterController controller = new SiteRegisterController();
- controller.username = 'test@force.com';
- controller.email = 'test@force.com';
- controller.communityNickname = 'test';
- // registerUser always returns null when the page isn't accessed as a guest user
- System.assert(controller.registerUser() == null);
- controller.password = 'abcd1234';
- controller.confirmPassword = 'abcd123';
- System.assert(controller.registerUser() == null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement