Class: LesliGuard::UsersController

Inherits:
Lesli::ApplicationLesliController
  • Object
show all
Defined in:
app/controllers/lesli_guard/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#becomeObject



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
# File 'app/controllers/lesli_guard/users_controller.rb', line 120

def become
    
    # always should be disabled
    if Rails.configuration.lesli.dig(:security, :enable_becoming) != true
        return respond_with_unauthorized
    end
    
    # Allow only sysadmin to become as user
    return respond_with_unauthorized if current_user.email != Rails.application.config.lesli.dig(:account, :user, :email) # sysadmin user
    
    # Search for desire user
    becoming_user = User.find(params[:id])
    
    # Return an error if user does not exist
    return respond_with_error I18n.t("core.shared.messages_warning_user_not_found") if becoming_user.blank?
    
    # Extrictly save a log when becoming
    current_user.activities.create!({
        users_id: becoming_user.id,
        owner_id: current_user.id,
        description: "#{current_user.full_name} -> #{becoming_user.full_name}",
        category: "action_become"
    })
    
    # Sign in as the becoming user
    (:user, becoming_user)
    
    # Create a new session for the becoming user
    becoming_session = becoming_user.sessions.create({
        :user_agent => get_user_agent,
        :user_remote => request.remote_ip,
        :session_source => "become_session",
        :last_used_at => LC::Date.now,
        :expiration_at => 60.minutes.from_now
    })
    
    # assign the session of the becomer user to the becoming user
    session[:user_session_id] = becoming_session[:id]
    
    # Response successful
    respond_with_successful([current_user, becoming_user])
    
end

#createObject



42
43
44
45
46
47
48
49
# File 'app/controllers/lesli_guard/users_controller.rb', line 42

def create
    user = UserService.new(current_user).create(user_params)
    if user.successful?
        respond_with_successful(user.result)
    else 
        respond_with_error("Error creating user", user.errors)
    end
end

#destroyObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/lesli_guard/users_controller.rb', line 67

def destroy
    return respond_with_not_found unless @user
    
    # get the user found in the UserServices
    user = @user.result
    
    if user.delete
        current_user.logs.create({ title: "deleted_user", description: "by_user_id: #{ current_user.email }" })
        respond_with_successful()
      else
        respond_with_error(user.errors.full_messages.to_sentence)
    end
end

#indexObject

GET /users



23
24
25
26
27
28
29
30
# File 'app/controllers/lesli_guard/users_controller.rb', line 23

def index
    respond_to do |format|
        format.html 
        format.json {
            return respond_with_pagination(UserService.new(current_user, query).index(params))
        }
    end
end

#listObject

GET /users/list



14
15
16
17
18
19
20
# File 'app/controllers/lesli_guard/users_controller.rb', line 14

def list
    respond_to do |format|
        format.json { 
            respond_with_successful(UserService.new("current_user").list(query, params)) 
        }
    end
end

#logoutObject

this method is going to close all the open user sessions



103
104
105
106
107
108
109
# File 'app/controllers/lesli_guard/users_controller.rb', line 103

def logout
    if @user.logout
        respond_with_successful
    else 
        respond_with_error
    end
end

#optionsObject



164
165
166
167
168
169
170
171
# File 'app/controllers/lesli_guard/users_controller.rb', line 164

def options
    respond_with_successful({
        #regions: current_user.account.locations.where(level: "region"),
        salutations: User::Detail.salutations.map {|k, v| {value: k, text: v}},
        locales: Rails.application.config.lesli.dig(:configuration, :locales_available),
        mfa_methods: Rails.application.config.lesli.dig(:configuration, :mfa_methods)
    })
end

#passwordresetObject

Reset password (generate random)



91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/lesli_guard/users_controller.rb', line 91

def passwordreset
    
    pass = @user.password_reset
    
    if pass
        respond_with_successful(pass)
    else 
        respond_with_error
    end
end

#requestpasswordObject

Force the user to update his password



82
83
84
85
86
87
88
# File 'app/controllers/lesli_guard/users_controller.rb', line 82

def requestpassword
    if @user.request_password
        respond_with_successful
    else 
        respond_with_error
    end
end

#revokeaccessObject

Remove all user access



112
113
114
115
116
117
118
# File 'app/controllers/lesli_guard/users_controller.rb', line 112

def revokeaccess
    if @user.revoke_access
        respond_with_successful
    else 
        respond_with_error
    end
end

#showObject



32
33
34
35
36
37
38
39
40
# File 'app/controllers/lesli_guard/users_controller.rb', line 32

def show
    respond_to do |format|
        format.html
        format.json {
            return respond_with_not_found unless @user.found?
            return respond_with_successful(@user.show)
        }
    end
end

#updateObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/lesli_guard/users_controller.rb', line 51

def update

    # check if the user trully exists
    return respond_with_not_found unless @user.found?

    # update the user information
    @user.update(user_params)

    # check saved
    if @user.successful?
        respond_with_successful(@user.result)
    else 
        respond_with_error(@user.errors)
    end
end