Class: UsersController

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

Constant Summary

Constants included from Searchable

Searchable::ABSTRACT

Instance Method Summary collapse

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Methods included from JsonHelper

#merge_notes, #process_json_notes

Methods included from Searchable

#default_search_opts, #get_filter_years, #handle_results, #html_notes, #process_results, #process_search_results, #repo_context, #search_terms, #set_up_advanced_search, #set_up_and_run_search, #set_up_search, #strip_facet_fields

Methods included from HandleFaceting

#fetch_only_facets, #get_pretty_facet_value, #strip_facets

Methods included from ManipulateNode

#inheritance, #process_mixed_content, #strip_mixed_content

Instance Method Details

#activateObject



195
196
197
198
199
200
201
202
# File 'frontend/app/controllers/users_controller.rb', line 195

def activate
  if JSONModel::HTTP::get_json("/users/#{params[:id]}/activate")
    flash[:success] = I18n.t("user._frontend.messages.activated")
  else
    flash[:error] = I18n.t("user._frontend.messages.error_activate")
  end
  redirect_to :action => :index
end

#completeObject



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

def complete
  query = params[:query].strip
  if !query.empty?
    begin
      return render :json => JSONModel::HTTP::get_json("/users/complete", :query => params[:query])
    rescue
    end
  end

  render :json => []
end

#createObject



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

def create
  handle_crud(:instance => :user,
              :params_check => ->(obj, params) {
                ['password', 'confirm_password'].each do |field|
                  if params['user'][field].blank?
                    obj.add_error(field, "Can't be empty")
                  end
                end
                if params['user']['password'] != params['user']['confirm_password']
                  obj.add_error('confirm_password', "entered value didn't match password")
                end
                s = params['user']['username'].downcase.strip
                if s.blank?
                  obj.add_error('username', "Can't be empty")
                elsif s !~ /\A[a-zA-Z0-9\-_. @]+\z/ || s =~ /  +/
                  obj.add_error('username', 'invalid characters')
                end
              },
              :on_invalid => ->() {
                flash[:error] = I18n.t("user._frontend.messages.error_create")
                render :action => "new"
              },
              :on_valid => ->(id) {
                if session[:user]
                  flash[:success] = "#{I18n.t("user._frontend.messages.created")}: #{params['user']['username']}"
                  redirect_to :controller => :users, :action => :index
                else
                  backend_session = User.(params['user']['username'],
                                             params['user']['password'])

                  User.establish_session(self, backend_session, params['user']['username'])

                  redirect_to :controller => :welcome, :action => :index

                end
              })
end

#current_recordObject



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

def current_record
  @user
end

#deactivateObject



204
205
206
207
208
209
210
211
# File 'frontend/app/controllers/users_controller.rb', line 204

def deactivate
  if JSONModel::HTTP::get_json("/users/#{params[:id]}/deactivate")
    flash[:success] = I18n.t("user._frontend.messages.deactivated")
  else
    flash[:error] = I18n.t("user._frontend.messages.error_deactivate")
  end
  redirect_to :action => :index
end

#deleteObject



87
88
89
90
91
92
93
# File 'frontend/app/controllers/users_controller.rb', line 87

def delete
  user = JSONModel(:user).find(params[:id])
  user.delete

  flash[:success] = I18n.t("user._frontend.messages.deleted", JSONModelI18nWrapper.new(:user => user))
  redirect_to(:controller => :users, :action => :index, :deleted_uri => user.uri)
end

#editObject



57
58
59
60
61
62
63
64
65
66
# File 'frontend/app/controllers/users_controller.rb', line 57

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

  if @user.is_system_user and not user_is_global_admin?
    flash[:error] = I18n.t("user._frontend.messages.access_denied", JSONModelI18nWrapper.new(:user => @user))
    redirect_to(:controller => :users, :action => :index) and return
  end

  render action: "edit"
end

#edit_groupsObject



75
76
77
78
79
80
81
82
83
84
85
# File 'frontend/app/controllers/users_controller.rb', line 75

def edit_groups
  @user = JSONModel(:user).from_hash(JSONModel::HTTP::get_json("/repositories/#{session[:repo_id]}/users/#{params[:id]}"))

  if @user.is_system_user or @user.is_admin
    flash[:error] = I18n.t("user._frontend.messages.group_not_required", JSONModelI18nWrapper.new(:user => @user))
    redirect_to(:controller => :users, :action => :index) and return
  end

  @groups = JSONModel(:group).all
  render action: "edit_groups"
end

#edit_selfObject



68
69
70
71
72
73
# File 'frontend/app/controllers/users_controller.rb', line 68

def edit_self
  @user = JSONModel(:user).find('current-user')
  @self_edit = true

  render action: "edit_self"
end

#indexObject



12
13
14
15
16
17
18
19
# File 'frontend/app/controllers/users_controller.rb', line 12

def index
  @search_data = JSONModel(:user).all(
    page: selected_page,
    page_size: 50,
    sort_field: params.fetch(:sort, :username),
    sort_direction: params.fetch(:direction, :asc)
  )
end

#manage_accessObject



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

def manage_access
  @search_data = JSONModel(:user).all(:page => selected_page)
  @manage_access = true
  render :action => "index"
end

#newObject



36
37
38
39
40
41
42
43
# File 'frontend/app/controllers/users_controller.rb', line 36

def new
  if AppConfig[:allow_user_registration] or (session['user'] and user_can? 'manage_users')
    @user = JSONModel(:user).new._always_valid!
    render action: "new"
  else
    redirect_to(:controller => :welcome, :action => :index)
  end
end

#showObject



31
32
33
34
# File 'frontend/app/controllers/users_controller.rb', line 31

def show
  @user = JSONModel(:user).find(params[:id])
  render action: "show"
end

#updateObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'frontend/app/controllers/users_controller.rb', line 95

def update
  handle_crud(:instance => :user,
              :obj => JSONModel(:user).find(params[:id]),
              :params_check => ->(obj, params) {
                if params['user']['password'] || params['user']['confirm_password']
                  if params['user']['password'] != params['user']['confirm_password']
                    obj.add_error('confirm_password', "entered value didn't match password")
                  end
                end
              },
              :on_invalid => ->() {
                flash[:error] = I18n.t("user._frontend.messages.error_update")
                render :action => "edit"
              },
              :on_valid => ->(id) {
                flash[:success] = I18n.t("user._frontend.messages.updated")
                redirect_to :action => :index
              })
end

#update_groupsObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'frontend/app/controllers/users_controller.rb', line 135

def update_groups
  groups = Array(params[:groups])

  uri = "/users/#{params[:id]}/groups"
  response = JSONModel::HTTP.post_form(URI(uri),
                                       'groups[]' => groups,
                                       :remove_groups => groups.empty?,
                                       :repo_id => session[:repo_id]
                                       )

  if response.code === '200'
    flash[:success] = I18n.t("user._frontend.messages.updated")
    redirect_to :action => :index
  else
    flash[:error] = I18n.t("user._frontend.messages.error_update")
    @groups = JSONModel(:group).all if user_can?('manage_repository')

    render :action => :edit_groups
  end
end

#update_selfObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'frontend/app/controllers/users_controller.rb', line 115

def update_self
  handle_crud(:instance => :user,
              :obj => obj = JSONModel(:user).find('current-user'),
              :params_check => ->(obj, params) {
                if params['user']['password'] || params['user']['confirm_password']
                  if params['user']['password'] != params['user']['confirm_password']
                    obj.add_error('confirm_password', "entered value didn't match password")
                  end
                end
              },
              :on_invalid => ->() {
                flash[:error] = I18n.t("user._frontend.messages.error_update")
                render :action => "edit_self"
              },
              :on_valid => ->(id) {
                flash[:success] = I18n.t("user._frontend.messages.updated")
                redirect_to :action => "edit_self"
              })
end