Class: Admin::UsersController

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

Instance Method Summary collapse

Instance Method Details

#editObject


12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/admin/users_controller.rb', line 12

def edit
  @user_roles = @user.roles.where(name: %w[administrator manager internal])
  @all_roles = Role.distinct.pluck(:name)
  @users_roles = @user.study_and_project_roles.order(name: :asc)
  @studies = Study.order(:id)
  @projects = Project.order(:id)

  respond_to do |format|
    format.js
    format.html
  end
end

#filterObject


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

def filter
  if params[:q]
    @users = User.order(:login).where('first_name LIKE :query OR last_name LIKE :query OR login LIKE :query', query: "%#{params[:q].downcase}%")
  end

  render partial: 'users', locals: { users: @users }
end

#grant_user_roleObject


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/admin/users_controller.rb', line 54

def grant_user_role
  if request.xhr?
    if params[:role]
      authorizable_object = if params[:role][:authorizable_type] == 'Project'
                              Project.find(params[:role][:authorizable_id])
                            else
                              Study.find(params[:role][:authorizable_id])
                            end
      @user.has_role(params[:role][:authorizable_name].to_s, authorizable_object)
      @users_roles = @user.study_and_project_roles.order(name: :asc)

      flash[:notice] = 'Role added'
      render partial: 'roles', status: 200
    else
      @users_roles = @user.study_and_project_roles.order(name: :asc)
      flash[:error] = 'A problem occurred while adding the role'
      render partial: 'roles', status: 500
    end
  else
    @users_roles = @user.study_and_project_roles.sort_by(&:name)
    flash[:error] = 'A problem occurred while adding the role'
    render partial: 'roles', status: 401
  end
end

#indexObject


8
9
10
# File 'app/controllers/admin/users_controller.rb', line 8

def index
  @users = User.order(:login)
end

#remove_user_roleObject


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/admin/users_controller.rb', line 79

def remove_user_role
  if request.xhr?
    if params[:role]
      authorizable_object = if params[:role][:authorizable_type] == 'project'
                              Project.find(params[:role][:authorizable_id])
                            else
                              Study.find(params[:role][:authorizable_id])
                            end
      @user.has_no_role(params[:role][:authorizable_name].to_s, authorizable_object)
      @users_roles = @user.study_and_project_roles.order(name: :asc)

      flash[:error] = 'Role was removed'
      render partial: 'roles', status: 200
    else
      @users_roles = @user.study_and_project_roles.order(name: :asc)
      flash[:error] = 'A problem occurred while removing the role'
      render partial: 'roles', status: 500
    end
  else
    @users_roles = @user.study_and_project_roles.order(name: :asc)
    flash[:error] = 'A problem occurred while removing the role'
    render partial: 'roles', status: 401
  end
end

#showObject


25
26
# File 'app/controllers/admin/users_controller.rb', line 25

def show
end

#switchObject


28
29
30
31
# File 'app/controllers/admin/users_controller.rb', line 28

def switch
  session[:user] = params[:id]
  redirect_to studies_url
end

#updateObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/admin/users_controller.rb', line 33

def update
  @user = User.find(params[:id])
  Role.general_roles.each do |role|
    if params[:role] && params[:role][role.name]
      @user.has_role(role.name)
    else
      @user.has_no_role(role.name)
    end
  end

  if @user.id == params[:id].to_i
    @user.update(params[:user])
  end
  if @user.save
    flash[:notice] = 'Profile updated'
  else
    flash[:error] = 'Problem updating profile'
  end
  redirect_to profile_path(@user)
end