Method: LesliGuard::UserService#create

Defined in:
app/services/lesli_guard/user_service.rb

#create(user_params) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/services/lesli_guard/user_service.rb', line 119

def create user_params

    # check if request has an email to create the user
    if user_params[:email].blank?
        self.error(I18n.t("core.users.messages_danger_not_valid_email_found"))
    end

    
    # register the new user
    user = User.new({
        :active => true,
        :email => user_params[:email],
        :alias => user_params[:alias] || "",
        :first_name => user_params[:first_name] || "",
        :last_name => user_params[:last_name] || "",
        :telephone => user_params[:telephone] || "",
        #:detail_attributes => user_params[:detail_attributes] || {}
    })



    # assign a random password
    user.password = Devise.friendly_token

    # enrol user to my own account
    user. = current_user.

    # users created through the administration area does not need to confirm their accounts
    # instead we send a password reset link, so they can have access to the platform
    #user.confirm

    if user.save

        # if a role is provided to assign to the new user
        # unless user_params[:roles_id].blank?
        #     # check if current user can work with the sent role
        #     if current_user.can_work_with_role?(user_params[:roles_id])
        #         # Search the role assigned
        #         role = current_user.account.roles.find_by(id: user_params[:roles_id])
        #         # assign role to the new user
        #         user.user_roles.create({ role: role })
        #     end
        # end

        # role validation - if new user does not have any role assigned
        # if user.roles.blank?

        #     default_role_id = current_user.account.settings.find_by(:name => "default_role_id")&.value
        #     owner_role_id =  current_user.account.roles.find_by(:name => "owner").id
        #     if default_role_id.present? && default_role_id != owner_role_id
        #         # assign default role
        #         user.user_roles.create({ role:  current_user.account.roles.find_by(:id => default_role_id)})

        #     else
        #         # assign limited role
        #         user.user_roles.create({ role: current_user.account.roles.find_by(:name => "limited") })
        #     end 
        # end

        # saving logs with information about the creation of the user
        # user.logs.create({ title: "user_created_at", description: Date2.new.date_time.to_s })
        # user.logs.create({ title: "user_created_by", description: current_user.email })
        # user.logs.create({ title: "user_created_with_role", description: user.user_roles.first.role.name + " " + user.user_roles.first.role.id.to_s})
        # User.log_activity_create(current_user, user)

        self.resource = user

        begin
            # users created through the administration area does not need to confirm their accounts
            # instead we send a password reset link, so they can have access to the platform
            #UserMailer.with(user: user).invitation_instructions.deliver_now
        rescue => exception
            #Honeybadger.notify(exception)
            #user.logs.create({ title: "user_creation_email_failed ", description: exception.message })
        end

    else
        self.error(user.errors.full_messages.to_sentence)
    end

    self

end