Class: LesliShield::UserRegistrationService

Inherits:
Lesli::ApplicationLesliService
  • Object
show all
Defined in:
app/services/lesli_shield/user_registration_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_user) ⇒ UserRegistrationService

Returns a new instance of UserRegistrationService.



36
37
38
39
# File 'app/services/lesli_shield/user_registration_service.rb', line 36

def initialize(current_user)
    @resource = current_user
    @current_user = current_user
end

Instance Method Details

#confirmObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/lesli_shield/user_registration_service.rb', line 41

def confirm

    # confirm the user
    current_user.confirm


    # force token deletion so we are sure nobody 
    # will be able to use the token again
    current_user.update(confirmation_token: nil)

    # Minimum security settings required
    #self.settings.create_with(:value => false).find_or_create_by(:name => "mfa_enabled")
    #self.settings.create_with(:value => :email).find_or_create_by(:name => "mfa_method")

end

#create_accountObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/services/lesli_shield/user_registration_service.rb', line 57

def 

    # check if instance is for multi-account
    allow_multiaccount = Lesli.config.security.dig(:allow_multiaccount)

    # create new account for the new user only if multi-account is allowed
    if allow_multiaccount === true
         = Lesli::Account.create!({
            user: resource,     # set user as owner of his just created account
            name: "Lesli",      # temporary company name
            email: resource.email,
            status: :active     # account is active due user already confirmed his email
        })
    end

    # if multi-account is not allowed user belongs to the first account in instance
    if allow_multiaccount === false
         = Lesli::Account.first
    end

    # add user to his own account
    resource. = 

    # add owner role to user only if multi-account is allowed
    if allow_multiaccount == true
        resource.user_roles.create({ role: .roles.find_by(name: "owner") })
    end

    # add profile role to user only if multi-account is allowed
    if allow_multiaccount == false
        # Assigning default role if defined in account settings
        # Otherwise, the default role is "limited"
        #default_role_id = account.settings.find_by(:name => "default_role_id")&.value
        default_role_id = nil
            
        if default_role_id.present?
            resource.roles.create({ role: .roles.find_by(:id => default_role_id)})
        else
            resource.roles.create({ role: .roles.find_by(name: "limited") })
        end
    end

    # Synchronize roles for the very first time
    resource.roles.each do |role|
        LesliShield::RolePrivilegeService.new(nil).synchronize(role)
    end

    # update user :)
    resource.save

    # # initialize user dependencies
    # def after_account_assignation
    # return unless self.account

    # #Courier::One::Firebase::User.sync_user(self)
    # # Lesli::Courier.new(:lesli_calendar).from(:calendar_service, self).create({
    # #     name: "Personal Calendar", 
    # #     default: true
    # # })
    # end

end