Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public without sharing class MyClass {
  2.  
  3. public String username{get;set;}
  4. public String password{get;set;}
  5. public PageReference logado {get;set;}
  6.  
  7. public pageReference myMethod(){
  8. logado = Site.login(username, password, null);
  9. if(logado != null){
  10. // code
  11. }
  12. }
  13. }
  14.  
  15. /**
  16. * An Apex class that creates a portal user
  17. */
  18. public class SiteRegisterController {
  19. // PORTAL_ACCOUNT_ID is the account on which the contact will be created on
  20. // and then enabled as a portal user.
  21. //Enter the account ID in place of <portal_account_id> below.
  22. private static Id PORTAL_ACCOUNT_ID = '<portal_account_id>';
  23. public SiteRegisterController () {
  24. }
  25. public String username {get; set;}
  26. public String email {get; set;}
  27. public String password {get; set {password = value == null ? value : value.trim(); } }
  28. public String confirmPassword {get; set { confirmPassword =
  29. value == null ? value : value.trim(); } }
  30. public String communityNickname {get; set { communityNickname =
  31. value == null ? value : value.trim(); } }
  32. private boolean isValidPassword() {
  33. return password == confirmPassword;
  34. }
  35. public PageReference registerUser() {
  36. // If password is null, a random password is sent to the user
  37. if (!isValidPassword()) {
  38. ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,
  39. Label.site.passwords_dont_match);
  40. ApexPages.addMessage(msg);
  41. return null;
  42. }
  43. User u = new User();
  44. u.Username = username;
  45. u.Email = email;
  46. u.CommunityNickname = communityNickname;
  47. String accountId = PORTAL_ACCOUNT_ID;
  48. // lastName is a required field on user, but if it isn't specified,
  49. the code uses the username
  50. String userId = Site.createPortalUser(u, accountId, password);
  51. if (userId != null) {
  52. if (password != null && password.length() > 1) {
  53. return Site.login(username, password, null);
  54. }
  55. else {
  56. PageReference page = System.Page.SiteRegisterConfirm;
  57. page.setRedirect(true);
  58. return page;
  59. }
  60. }
  61. return null;
  62. }
  63. }
  64.  
  65. /**
  66. * Test class.
  67. */
  68. @isTest
  69. private class SiteRegisterControllerTest {
  70. // Test method for verifying the positive test case
  71. static testMethod void testRegistration() {
  72. SiteRegisterController controller = new SiteRegisterController();
  73. controller.username = 'test@force.com';
  74. controller.email = 'test@force.com';
  75. controller.communityNickname = 'test';
  76. // registerUser always returns null when the page isn't accessed as a guest user
  77. System.assert(controller.registerUser() == null);
  78. controller.password = 'abcd1234';
  79. controller.confirmPassword = 'abcd123';
  80. System.assert(controller.registerUser() == null);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement