-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathusers_controller.rb
More file actions
145 lines (106 loc) · 2.89 KB
/
Copy pathusers_controller.rb
File metadata and controls
145 lines (106 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Admin::Api::UsersController < Admin::Api::BaseController
representer User
before_action :can_create, only: :create
before_action :build_new_user, only: %i[create]
before_action :find_user, except: %i[create index]
attr_reader :user
# User List (provider account)
# GET /admin/api/users.xml
def index
authorize! :manage, :multiple_users
respond_with(users)
end
# User Create (provider account)
# POST /admin/api/users.xml
def create
authorize! :create, user
user.update(user_params.merge(signup_type: :api))
respond_with(user)
end
# User Read (provider account)
# GET /admin/api/users/{id}.xml
def show
authorize! :show, user
respond_with(user)
end
# User Update (provider account)
# PUT /admin/api/users/{id}.xml
def update
authorize! :update, user
user.update(user_params)
respond_with(user)
end
# User Delete (provider account)
# DELETE /admin/api/users/{id}.xml
def destroy
authorize! :destroy, user
user.destroy
respond_with(user)
end
# User Change Role to Member (provider account)
# PUT /admin/api/users/{id}/member.xml
def member
authorize! :update_role, user
user.make_member
respond_with(user)
end
# User Change Role to Admin (provider account)
# PUT /admin/api/users/{id}/admin.xml
def admin
authorize! :update_role, user
user.make_admin
respond_with(user)
end
# User Suspend (provider account)
# PUT /admin/api/users/{id}/suspend.xml
def suspend
authorize! :suspend, user
user.suspend!
respond_with(user)
end
# User Unsuspend (provider account)
# PUT /admin/api/users/{id}/unsuspend.xml
def unsuspend
authorize! :unsuspend, user
user.unsuspend
respond_with(user)
end
# User Activate (provider account)
# PUT /admin/api/users/{id}/activate.xml
def activate
authorize! :update, user
user.activate
respond_with(user)
end
protected
def authorize!(*args)
current_user ? super : logged_in?
end
def users
@users ||= begin
conditions = params.slice(:state, :role)
current_account.users.but_impersonation_admin.where(conditions)
end
end
def build_new_user
@user = current_account.users.new
end
def find_user
@user = current_account.users.but_impersonation_admin.find(params[:id])
end
def can_create
head :forbidden unless current_account.can_create_user?
end
private
def flat_params
super.except(:id)
end
def user_params
@user_params ||= begin
permission_attrs = [member_permission_service_ids: [], member_permission_ids: [], allowed_sections: [], allowed_service_ids: []]
allowed_attrs = user.defined_fields_names | %i(password password_confirmation cas_identifier signup_type)
allowed_attrs |= permission_attrs if (provider_key.present? || current_user.admin?)
flat_params.permit(*allowed_attrs)
end
end
end