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



168
169
170
171
172
173
174
175
# File 'frontend/app/controllers/users_controller.rb', line 168

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



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

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



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

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



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

def current_record
  @user
end

#deactivateObject



177
178
179
180
181
182
183
184
# File 'frontend/app/controllers/users_controller.rb', line 177

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



80
81
82
83
84
85
86
# File 'frontend/app/controllers/users_controller.rb', line 80

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



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

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

#indexObject



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

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



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

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

#newObject



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

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



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

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

#updateObject



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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'frontend/app/controllers/users_controller.rb', line 108

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