Class: Incline::UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/incline/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#apiObject

GET/POST /incline/users/api?action=…



235
236
237
# File 'app/controllers/incline/users_controller.rb', line 235

def api
  process_api_action
end

#createObject

POST /incline/signup



33
34
35
36
37
38
39
40
41
42
43
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
# File 'app/controllers/incline/users_controller.rb', line 33

def create
  @user = Incline::User.new(user_params :before_create)

  if system_admin? # skip recaptcha check if an admin is currently logged in.
    @user.recaptcha = :verified
  end

  if @user.valid?
    if @user.save
      @user.send_activation_email request.remote_ip
      if system_admin?
        flash[:info] = "The user #{@user} has been created, but will need to activate their account before use."
        additional_params = user_params :after_create
        if additional_params.any?
          unless @user.update_attributes(additional_params)
            flash[:warning] = 'Failed to apply additional attributes to new user account.'
          end
        end
        if inline_request?
          render 'show', formats: [ :json ]
        else
          redirect_to users_url
        end
        return
      else
        flash[:safe_info] = 'Your account has been created, but needs to be activated before you can use it.<br>Please check your email to activate your account.'
        if inline_request?
          render 'show', formats: [ :json ]
        else
          redirect_to main_app.root_url
        end
        return
      end
    else
      @user.errors[:base] << 'Failed to create user account.'
    end
  end
  render 'new'
end

#demoteObject

PUT /incline/users/1/demote



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/controllers/incline/users_controller.rb', line 206

def demote
  # remove the administrator flag from the selected user.
  if @user.system_admin?
    if @user.update(system_admin: false)
      flash[:success] = "User #{@user} has been demoted from administrator."
    else
      flash[:danger] = "Failed to demote user #{@user}."
    end
  else
    flash[:warning] = "User #{@user} is not an administrator."
    unless inline_request?
      redirect_to users_path and return
    end
  end

  if inline_request?
    render 'show', formats: [ :json ]
  else
    redirect_to users_path
  end

end

#destroyObject

DELETE /incline/users/1



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/incline/users_controller.rb', line 112

def destroy
  if @user.enabled?
    flash[:danger] = 'Cannot delete an enabled user.'
  elsif @user.disabled_at.blank? || @user.disabled_at > 15.days.ago
    flash[:danger] = 'Cannot delete a user within 15 days of being disabled.'
  else
    @user.destroy
    flash[:success] = "User #{@user} has been deleted."
  end
  if inline_request?
    render 'show', formats: [ :json ]
  else
    redirect_to users_path
  end
end

#disableObject

PUT /incline/users/1/disable



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/incline/users_controller.rb', line 141

def disable
  if @disable_info.valid?
    if @disable_info.user.disable(current_user, @disable_info.reason)
      flash[:success] = "User #{@disable_info.user} has been disabled."
      if inline_request?
        render 'show', formats: [ :json ]
      else
        redirect_to users_path
      end
      return
    else
      @disable_info.errors.add(:user, 'was unable to be updated')
    end
  end
  render 'disable_confirm'
end

#disable_confirmObject

GET /incline/users/1/disable



130
131
132
133
134
135
136
137
# File 'app/controllers/incline/users_controller.rb', line 130

def disable_confirm
  unless @disable_info.user.enabled?
    flash[:warning] = "User #{@disable_info.user} is already disabled."
    unless inline_request?
      redirect_to users_path
    end
  end
end

#editObject

GET /incline/users/1/edit



81
82
83
# File 'app/controllers/incline/users_controller.rb', line 81

def edit
  render 'edit'
end

#enableObject

PUT /incline/users/1/enable



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/controllers/incline/users_controller.rb', line 160

def enable
  if @user.enabled?
    flash[:warning] = "User #{@user} is already enabled."
    unless inline_request?
      redirect_to users_path and return
    end
  else
    if @user.enable
      flash[:success] = "User #{@user} has been enabled."
    else
      flash[:danger] = "Failed to enable user #{@user}."
    end
  end
  if inline_request?
    render 'show', formats: [ :json ]
  else
    redirect_to users_path
  end
end

#indexObject

GET /incline/users



21
22
23
# File 'app/controllers/incline/users_controller.rb', line 21

def index

end

#locateObject

POST /incline/users/1/locate



230
231
232
# File 'app/controllers/incline/users_controller.rb', line 230

def locate
  render json: { record: @dt_request.record_location }
end

#newObject

GET /incline/signup



27
28
29
# File 'app/controllers/incline/users_controller.rb', line 27

def new
  @user = Incline::User.new
end

#promoteObject

PUT /incline/users/1/promote



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/controllers/incline/users_controller.rb', line 182

def promote
  # add the administrator flag to the selected user.
  if @user.system_admin?
    flash[:warning] = "User #{@user} is already an administrator."
    unless inline_request?
      redirect_to users_path and return
    end
  else
    if @user.update(system_admin: true)
      flash[:success] = "User #{@user} has been promoted to administrator."
    else
      flash[:danger] = "Failed to promote user #{@user}."
    end
  end

  if inline_request?
    render 'show', formats: [ :json ]
  else
    redirect_to users_path
  end
end

#showObject

GET /incline/users/1



75
76
77
# File 'app/controllers/incline/users_controller.rb', line 75

def show
  render 'show'
end

#updateObject

PUT /incline/users/1



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/incline/users_controller.rb', line 87

def update
  if @user.update_attributes(user_params)
    if current_user?(@user)
      flash[:success] = 'Your profile has been updated.'
      if inline_request?
        render 'show', formats: [ :json ]
      else
        redirect_to @user
      end
      return
    else
      flash[:success] = "The user #{@user} has been updated."
      if inline_request?
        render 'show', formats: [ :json ]
      else
        redirect_to users_path
      end
      return
    end
  end
  render 'edit'
end