Class: Lesli::UserRegistrationOperator

Inherits:
ApplicationLesliService show all
Defined in:
app/operators/lesli/user_registration_operator.rb

Instance Method Summary collapse

Methods inherited from ApplicationLesliService

#create, #delete, #error, #errors, #errors_as_sentence, #find, #found?, #index, #list, #result, #show, #successful?, #update

Constructor Details

#initialize(current_user) ⇒ UserRegistrationOperator

Returns a new instance of UserRegistrationOperator.



37
38
39
40
# File 'app/operators/lesli/user_registration_operator.rb', line 37

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

Instance Method Details

#confirmObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/operators/lesli/user_registration_operator.rb', line 42

def confirm

    if current_user.blank?
        failures.push(I18n.t("core.shared.messages_warning_user_not_found")) 
        return self
    end

    # confirm the user
    current_user.confirm

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

    # send a welcome email to user as is confirmed
    #UserMailer.with(user: resource).welcome.deliver_later

    # initialize user dependencies
    current_user.after_confirmation_user

end

#create_accountObject



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
119
# File 'app/operators/lesli/user_registration_operator.rb', line 63

def 

    if resource.blank?
        failures.push(I18n.t("core.shared.messages_warning_user_not_found")) 
        return self
    end

    if resource.
        failures.push(I18n.t("core.users.messages_info_user_already_belongs_to_account")) 
        return self
    end

    # 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
         = Account.create!({
            user: resource,     # set user as owner of his just created account
            name: "Lesli",      # temporary company name
            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
         = 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.powers.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 = .settings.find_by(:name => "default_role_id")&.value
            
        if default_role_id.present?
            resource.user_roles.create({ role: .roles.find_by(:id => default_role_id)})
        else
            resource.user_roles.create({ role: .roles.find_by(name: "limited") })
        end
    end

    # update user :)
    resource.save

    # initialize user dependencies
    resource.

end