Class: UsersController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#is_data_controller, #is_task_controller, #meta_data, #meta_description, #meta_keywords, #meta_title, #page_title, #site_name

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#clear_project_and_user_variables, #digest_cookie, #digested_cookie_exists?, #disable_turbolinks, #intercept_api, #invalid_object, #is_data_controller?, #is_task_controller?, #log_user_recent_route, #notice_user, #project_matches, #record_not_found, #set_project_and_user_variables, #set_project_from_params, #token_authenticate

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Instance Method Details

- (Object) create

POST /users



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

def create
  @user = User.new(user_params)
  @user.is_flagged_for_password_reset = is_superuser?

  if @user.save
    flash[:success] = "User #{@user.email} successfully created."
    # TODO: Email the user their information.
    redirect_to root_path
  else
    render 'new'
  end
end

- (Object) destroy

DELETE /users/:id



57
58
59
60
61
# File 'app/controllers/users_controller.rb', line 57

def destroy
  User.find(params[:id]).destroy
  flash[:success] = 'Account has been deleted.'
  redirect_to root_url
end

- (Object) edit

GET /users/:id/edit



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

def edit
  @user = User.find(params[:id])
end

- (Object) forgot_password

GET /forgot_password



64
65
# File 'app/controllers/users_controller.rb', line 64

def forgot_password
end

- (Object) index

GET /users



12
13
14
# File 'app/controllers/users_controller.rb', line 12

def index
  @users = User.all.order(:name, :email)
end

- (Object) new

GET /signup



17
18
19
# File 'app/controllers/users_controller.rb', line 17

def new
  @user = User.new
end

- (Object) password_reset

GET /password_reset



90
91
92
93
# File 'app/controllers/users_controller.rb', line 90

def password_reset
  @user = User.find_by_password_reset_token(RandomToken.digest(params[:token]))
  render 'invalid_token.html.erb' unless @user && @user.password_reset_token_date > 1.day.ago
end

- (Object) recently_created



110
111
# File 'app/controllers/users_controller.rb', line 110

def recently_created
end

- (Object) recently_created_stats



113
114
115
# File 'app/controllers/users_controller.rb', line 113

def recently_created_stats
  render json: @user.data_breakdown_for_chartkick_recent
end

- (Object) send_password_reset

POST /send_password_reset



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/users_controller.rb', line 68

def send_password_reset
  if params[:email] 
    user = User.find_by_email(params[:email].downcase)
  end
  
  if user.nil? 
    redirect_to :forgot_password
  
    if params[:email].blank?
      flash[:notice] = 'No e-mail was given'
    else
      flash[:notice] = 'The supplied e-mail does not belong to a registered user'
    end
  else
    token = user.generate_password_reset_token
    $user_id = user.id
    user.save
    UserMailer.password_reset_email(user, token).deliver_now
  end
end

- (Object) set_password

PATCH /set_password



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/users_controller.rb', line 96

def set_password
  @user = User.find_by_password_reset_token!(RandomToken.digest(params[:token]))
  $user_id = @user.id
  @user.require_password_presence
  @user.password_reset_token = nil
  @user.is_flagged_for_password_reset = false
  if @user.update_attributes(params.require(:user).permit([:password, :password_confirmation]))
    flash[:success] = 'Password successfuly changed.'
    redirect_to root_path
  else
    render 'password_reset.html.erb'
  end
end

- (Object) set_user (private)



133
134
135
136
# File 'app/controllers/users_controller.rb', line 133

def set_user
  @user = User.find(params[:id])
  @recent_object = @user 
end

- (Object) show

GET /users/:id



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

def show
  @user = User.find(params[:id])
end

- (Object) update

PATCH or PUT /users/:id



46
47
48
49
50
51
52
53
54
# File 'app/controllers/users_controller.rb', line 46

def update
  @user = User.find(params[:id])
  if @user.update_attributes(user_params)
    flash[:success] = 'Changes to your account information have been saved.'
    redirect_to @user
  else
    render 'edit'
  end
end

- (Object) user_params (private)



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/users_controller.rb', line 119

def user_params
  # TODO: revisit authorization of specific field settings
  basic = [:name,
  :email,
  :password,
  :password_confirmation,
  :set_new_api_access_token]

  basic.push [:is_project_administrator, :is_flagged_for_password_reset] if is_superuser?
  basic.push [:is_administrator] if is_administrator?

  params.require(:user).permit(basic)
end

- (Object) validate_user_id_belongs_to_user_or_require_a_superuser (private)



138
139
140
# File 'app/controllers/users_controller.rb', line 138

def validate_user_id_belongs_to_user_or_require_a_superuser
  (@user.id == sessions_current_user_id) || is_superuser?
end