Class: LesliGuard::UserService
- Inherits:
-
Lesli::ApplicationLesliService
- Object
- Lesli::ApplicationLesliService
- LesliGuard::UserService
- Defined in:
- app/services/lesli_guard/user_service.rb
Instance Method Summary collapse
- #available_roles ⇒ Object
- #create(user_params) ⇒ Object
- #find(id) ⇒ Object
-
#index(params) ⇒ Array
TODO: Implement pg_search.
- #logout ⇒ Object
-
#password_reset ⇒ Object
generate a random password for the user.
-
#request_password ⇒ Object
force the user to change the password (at next login).
- #revoke_access ⇒ Object
- #sessions(current_session_id) ⇒ Object
-
#show ⇒ Object
Creates a query that selects all user information from several tables if CloudLock is present.
- #update(params) ⇒ Object
Instance Method Details
#available_roles ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'app/services/lesli_guard/user_service.rb', line 279 def available_roles roles = current_user.account.roles .joins(%( left join lesli_user_powers on lesli_user_powers.role_id = lesli_roles.id and lesli_user_powers.deleted_at is null and lesli_user_powers.user_id = #{ resource.id } )) #.where("object_level_permission < ?", current_user.max_object_level_permission) .order(object_level_permission: :desc) .select( "coalesce(lesli_roles.id, lesli_user_powers.role_id) as id", "name", "description", "object_level_permission", "case when lesli_user_powers.role_id is null then false else true end as active" ) # only owner can assign any role #unless self.has_roles?("owner") # roles = roles.where("object_level_permission < ?", (self.roles.map{ |r| r[:object_level_permission] }).max) #end roles || [] end |
#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.account = current_user.account # 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..to_sentence) end self end |
#find(id) ⇒ Object
36 37 38 39 |
# File 'app/services/lesli_guard/user_service.rb', line 36 def find id #super(current_user.account.users.joins(:detail).find_by(id: id)) super(current_user.account.users.find_by(id: id)) end |
#index(params) ⇒ Array
TODO: Implement pg_search
44 45 46 47 48 49 50 51 52 53 54 55 56 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 |
# File 'app/services/lesli_guard/user_service.rb', line 44 def index params # sql string to join to user_roles and get all the roles assigned to a user sql_string_for_user_roles = "left join ( select ur.user_id, string_agg(r.\"name\", ', ') rolenames from lesli_user_powers ur join lesli_roles r on r.id = ur.role_id where ur.deleted_at is null group by ur.user_id ) roles on roles.user_id = lesli_users.id" # sql string to joing to user_sessions and get all the active sessions of a user sql_string_for_user_sessions = "left join ( select max(last_used_at) as last_action_performed_at, user_id from lesli_user_sessions us where us.deleted_at is null group by(us.user_id) ) sessions on sessions.user_id = lesli_users.id" users = current_user.account.users .joins(sql_string_for_user_roles) #.joins(sql_string_for_user_sessions) users = users.page(query[:pagination][:page]) .per(query[:pagination][:perPage]) .order("#{query[:order][:by]} #{query[:order][:dir]} NULLS LAST") users.select( :id, "CONCAT(COALESCE(first_name, ''), ' ', COALESCE(last_name, '')) as name", :email, :active, :rolenames, Date2.new.date_time.db_column("current_sign_in_at") ) end |
#logout ⇒ Object
243 244 245 246 247 248 |
# File 'app/services/lesli_guard/user_service.rb', line 243 def logout # delete user active sessions resource.sessions.destroy_all resource.logs.create({ title: "close_sessions", description: "by_user: " + current_user.email }) end |
#password_reset ⇒ Object
generate a random password for the user
233 234 235 236 237 238 239 240 241 |
# File 'app/services/lesli_guard/user_service.rb', line 233 def password_reset # generate random password pass = resource.password_reset resource.logs.create({ title: "password_reset", description: "by_user: " + current_user.email }) pass end |
#request_password ⇒ Object
force the user to change the password (at next login)
223 224 225 226 227 228 229 |
# File 'app/services/lesli_guard/user_service.rb', line 223 def request_password # expire password resource.set_password_as_expired resource.logs.create({ title: "request_password", description: "by_user: " + current_user.email }) end |
#revoke_access ⇒ Object
250 251 252 253 254 255 256 257 258 259 |
# File 'app/services/lesli_guard/user_service.rb', line 250 def revoke_access # delete user active sessions self.logout # add delete date to the last active session resource.revoke_access resource.logs.create({ title: "revoke_access", description: "by_user: " + current_user.email }) end |
#sessions(current_session_id) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'app/services/lesli_guard/user_service.rb', line 261 def sessions(current_session_id) current_user.sessions .joins(:user) .where("expiration_at > ? or expiration_at is ?", Time.now.utc, nil) .select( :id, :session_source, Date2.new.date_time.db_column("created_at", "lesli_user_sessions"), Date2.new.date_time.db_column("last_used_at"), Date2.new.date_time.db_column("expiration_at"), "CONCAT_WS(' ', agent_platform, agent_os, '/', agent_browser, agent_version) as device", "case when #{current_session_id} = lesli_user_sessions.id then true else false end as current_session" ) .page(query[:pagination][:page]) .per(query[:pagination][:perPage]) .order(updated_at: :desc) end |
#show ⇒ Object
Creates a query that selects all user information from several tables if CloudLock is present
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 |
# File 'app/services/lesli_guard/user_service.rb', line 87 def show user = resource return { id: user[:id], email: user[:email], alias: user[:alias], active: user[:active], full_name: user.full_name, salutation: user[:salutation], first_name: user[:first_name], last_name: user[:last_name], telephone: user[:telephone], locale: user.locale, #settings.select(:value).find_by(:name => "locale"), roles: user.roles.map { |r| { id: r[:id], name: r[:name], permission_level: r[:object_level_permission]} }, #mfa_enabled: user.mfa_settings[:enabled], #mfa_method: user.mfa_settings[:method], created_at: user[:created_at], updated_at: user[:updated_at], detail_attributes: { title: user.detail[:title] || "", address: user.detail[:address], # work_city: user.detail[:work_city], # work_region: user.detail[:work_region], # work_address: user.detail[:work_address] } } end |
#update(params) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'app/services/lesli_guard/user_service.rb', line 203 def update params # old_attributes = resource.detail.attributes.merge({ # active: resource.active # }) if resource.update(params) # new_attributes = resource.detail.attributes.merge({ # active: resource.active # }) #resource.log_activity_update(current_user, resource, old_attributes, new_attributes) else self.error(resource.errors..to_sentence) end self end |