users_controller.rb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class UsersController < ApplicationController
  3. before_action :authentication_check, except: [:create, :password_reset_send, :password_reset_verify, :image]
  4. # @path [GET] /users
  5. #
  6. # @summary Returns a list of User records.
  7. # @notes The requester has to be in the role 'Admin' or 'Agent' to
  8. # get a list of all Users. If the requester is in the
  9. # role 'Customer' only just the own User record will be returned.
  10. #
  11. # @response_message 200 [Array<User>] List of matching User records.
  12. # @response_message 401 Invalid session.
  13. def index
  14. # only allow customer to fetch him self
  15. users = if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?('Agent')
  16. User.where(id: current_user.id)
  17. else
  18. User.all
  19. end
  20. users_all = []
  21. users.each {|user|
  22. users_all.push User.lookup(id: user.id).attributes_with_associations
  23. }
  24. render json: users_all, status: :ok
  25. end
  26. # @path [GET] /users/{id}
  27. #
  28. # @summary Returns the User record with the requested identifier.
  29. # @notes The requester has to be in the role 'Admin' or 'Agent' to
  30. # access all User records. If the requester is in the
  31. # role 'Customer' just the own User record is accessable.
  32. #
  33. # @parameter id(required) [Integer] The identifier matching the requested User.
  34. # @parameter full [Bool] If set a Asset structure with all connected Assets gets returned.
  35. #
  36. # @response_message 200 [User] User record matching the requested identifier.
  37. # @response_message 401 Invalid session.
  38. def show
  39. # access deny
  40. return if !permission_check
  41. if params[:full]
  42. full = User.full(params[:id])
  43. render json: full
  44. return
  45. end
  46. user = User.find(params[:id])
  47. render json: user
  48. end
  49. # @path [POST] /users
  50. #
  51. # @summary Creates a User record with the provided attribute values.
  52. # @notes TODO.
  53. #
  54. # @parameter User(required,body) [User] The attribute value structure needed to create a User record.
  55. #
  56. # @response_message 200 [User] Created User record.
  57. # @response_message 401 Invalid session.
  58. def create
  59. user = User.new( User.param_cleanup(params, true) )
  60. begin
  61. # check if it's first user
  62. count = User.all.count()
  63. # if it's a signup, add user to customer role
  64. if !current_user
  65. user.updated_by_id = 1
  66. user.created_by_id = 1
  67. # check if feature is enabled
  68. if !Setting.get('user_create_account')
  69. render json: { error_human: 'Feature not enabled!' }, status: :unprocessable_entity
  70. return
  71. end
  72. # add first user as admin/agent and to all groups
  73. group_ids = []
  74. role_ids = []
  75. if count <= 2
  76. Role.where(name: [ Z_ROLENAME_ADMIN, 'Agent', 'Chat']).each { |role|
  77. role_ids.push role.id
  78. }
  79. Group.all().each { |group|
  80. group_ids.push group.id
  81. }
  82. # everybody else will go as customer per default
  83. else
  84. role_ids.push Role.where(name: Z_ROLENAME_CUSTOMER).first.id
  85. end
  86. user.role_ids = role_ids
  87. user.group_ids = group_ids
  88. # else do assignment as defined
  89. else
  90. # permission check by role
  91. return if !permission_check_by_role(params)
  92. if params[:role_ids]
  93. user.role_ids = params[:role_ids]
  94. end
  95. if params[:group_ids]
  96. user.group_ids = params[:group_ids]
  97. end
  98. end
  99. # check if user already exists
  100. if user.email
  101. exists = User.where(email: user.email.downcase).first
  102. if exists
  103. render json: { error_human: 'User already exists!' }, status: :unprocessable_entity
  104. return
  105. end
  106. end
  107. user.save!
  108. # if first user was added, set system init done
  109. if count <= 2
  110. Setting.set('system_init_done', true)
  111. # fetch org logo
  112. if user.email
  113. Service::Image.organization_suggest(user.email)
  114. end
  115. end
  116. # send inviteation if needed / only if session exists
  117. if params[:invite] && current_user
  118. # generate token
  119. token = Token.create(action: 'PasswordReset', user_id: user.id)
  120. # send mail
  121. data = {}
  122. data[:subject] = 'Invitation to #{config.product_name} at #{config.fqdn}'
  123. data[:body] = 'Hi #{user.firstname},
  124. I (#{current_user.firstname} #{current_user.lastname}) invite you to #{config.product_name} - the customer support / ticket system platform.
  125. Click on the following link and set your password:
  126. #{config.http_type}://#{config.fqdn}/#password_reset_verify/#{token.name}
  127. Enjoy,
  128. #{current_user.firstname} #{current_user.lastname}
  129. Your #{config.product_name} Team
  130. '
  131. # prepare subject & body
  132. [:subject, :body].each { |key|
  133. data[key.to_sym] = NotificationFactory.build(
  134. locale: user.preferences[:locale],
  135. string: data[key.to_sym],
  136. objects: {
  137. token: token,
  138. user: user,
  139. current_user: current_user,
  140. }
  141. )
  142. }
  143. # send notification
  144. NotificationFactory.send(
  145. recipient: user,
  146. subject: data[:subject],
  147. body: data[:body]
  148. )
  149. end
  150. user_new = User.find(user.id)
  151. render json: user_new, status: :created
  152. rescue => e
  153. render json: model_match_error(e.message), status: :unprocessable_entity
  154. end
  155. end
  156. # @path [PUT] /users/{id}
  157. #
  158. # @summary Updates the User record matching the identifier with the provided attribute values.
  159. # @notes TODO.
  160. #
  161. # @parameter id(required) [Integer] The identifier matching the requested User record.
  162. # @parameter User(required,body) [User] The attribute value structure needed to update a User record.
  163. #
  164. # @response_message 200 [User] Updated User record.
  165. # @response_message 401 Invalid session.
  166. def update
  167. # access deny
  168. return if !permission_check
  169. user = User.find(params[:id])
  170. begin
  171. # permission check by role
  172. return if !permission_check_by_role(params)
  173. user.update_attributes( User.param_cleanup(params) )
  174. # only allow Admin's and Agent's
  175. if role?(Z_ROLENAME_ADMIN) && role?('Agent') && params[:role_ids]
  176. user.role_ids = params[:role_ids]
  177. end
  178. # only allow Admin's
  179. if role?(Z_ROLENAME_ADMIN) && params[:group_ids]
  180. user.group_ids = params[:group_ids]
  181. end
  182. # only allow Admin's and Agent's
  183. if role?(Z_ROLENAME_ADMIN) && role?('Agent') && params[:organization_ids]
  184. user.organization_ids = params[:organization_ids]
  185. end
  186. # get new data
  187. user_new = User.find( params[:id] )
  188. render json: user_new, status: :ok
  189. rescue => e
  190. render json: { error: e.message }, status: :unprocessable_entity
  191. end
  192. end
  193. # @path [DELETE] /users/{id}
  194. #
  195. # @summary Deletes the User record matching the given identifier.
  196. # @notes The requester has to be in the role 'Admin' to be able to delete a User record.
  197. #
  198. # @parameter id(required) [User] The identifier matching the requested User record.
  199. #
  200. # @response_message 200 User successfully deleted.
  201. # @response_message 401 Invalid session.
  202. def destroy
  203. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  204. model_destory_render(User, params)
  205. end
  206. # @path [GET] /users/search
  207. #
  208. # @tag Search
  209. # @tag User
  210. #
  211. # @summary Searches the User matching the given expression(s).
  212. # @notes TODO: It's possible to use the SOLR search syntax.
  213. # The requester has to be in the role 'Admin' or 'Agent' to
  214. # be able to search for User records.
  215. #
  216. # @parameter term [String] The search term.
  217. # @parameter limit [Integer] The limit of search results.
  218. # @parameter role_ids(multi) [Array<String>] A list of Role identifiers to which the Users have to be allocated to.
  219. # @parameter full [Boolean] Defines if the result should be
  220. # true: { user_ids => [1,2,...], assets => {...} }
  221. # or false: [{:id => user.id, :label => "firstname lastname <email>", :value => "firstname lastname <email>"},...].
  222. #
  223. # @response_message 200 [Array<User>] A list of User records matching the search term.
  224. # @response_message 401 Invalid session.
  225. def search
  226. if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN) && !role?('Agent')
  227. response_access_deny
  228. return
  229. end
  230. query_params = {
  231. query: params[:term],
  232. limit: params[:limit],
  233. current_user: current_user,
  234. }
  235. if params[:role_ids] && !params[:role_ids].empty?
  236. query_params[:role_ids] = params[:role_ids]
  237. end
  238. # do query
  239. user_all = User.search(query_params)
  240. # build result list
  241. if !params[:full]
  242. users = []
  243. user_all.each { |user|
  244. realname = user.firstname.to_s + ' ' + user.lastname.to_s
  245. if user.email && user.email.to_s != ''
  246. realname = realname + ' <' + user.email.to_s + '>'
  247. end
  248. a = { id: user.id, label: realname, value: realname }
  249. users.push a
  250. }
  251. # return result
  252. render json: users
  253. return
  254. end
  255. user_ids = []
  256. assets = {}
  257. user_all.each { |user|
  258. assets = user.assets(assets)
  259. user_ids.push user.id
  260. }
  261. # return result
  262. render json: {
  263. assets: assets,
  264. user_ids: user_ids.uniq,
  265. }
  266. end
  267. # @path [GET] /users/recent
  268. #
  269. # @tag Search
  270. # @tag User
  271. #
  272. # @summary Recent creates Users.
  273. # @notes Recent creates Users.
  274. #
  275. # @parameter limit [Integer] The limit of search results.
  276. # @parameter role_ids(multi) [Array<String>] A list of Role identifiers to which the Users have to be allocated to.
  277. # @parameter full [Boolean] Defines if the result should be
  278. # true: { user_ids => [1,2,...], assets => {...} }
  279. # or false: [{:id => user.id, :label => "firstname lastname <email>", :value => "firstname lastname <email>"},...].
  280. #
  281. # @response_message 200 [Array<User>] A list of User records matching the search term.
  282. # @response_message 401 Invalid session.
  283. def recent
  284. if role?(Z_ROLENAME_CUSTOMER) && !role?(Z_ROLENAME_ADMIN)
  285. response_access_deny
  286. return
  287. end
  288. # do query
  289. user_all = if params[:role_ids] && !params[:role_ids].empty?
  290. User.joins(:roles).where( 'roles.id' => params[:role_ids] ).where('users.id != 1').order('users.created_at DESC').limit( params[:limit] || 20 )
  291. else
  292. User.where('id != 1').order('created_at DESC').limit( params[:limit] || 20 )
  293. end
  294. # build result list
  295. if !params[:full]
  296. users = []
  297. user_all.each { |user|
  298. realname = user.firstname.to_s + ' ' + user.lastname.to_s
  299. if user.email && user.email.to_s != ''
  300. realname = realname + ' <' + user.email.to_s + '>'
  301. end
  302. a = { id: user.id, label: realname, value: realname }
  303. users.push a
  304. }
  305. # return result
  306. render json: users
  307. return
  308. end
  309. user_ids = []
  310. assets = {}
  311. user_all.each { |user|
  312. assets = user.assets(assets)
  313. user_ids.push user.id
  314. }
  315. # return result
  316. render json: {
  317. assets: assets,
  318. user_ids: user_ids.uniq,
  319. }
  320. end
  321. # @path [GET] /users/history/{id}
  322. #
  323. # @tag History
  324. # @tag User
  325. #
  326. # @summary Returns the History records of a User record matching the given identifier.
  327. # @notes The requester has to be in the role 'Admin' or 'Agent' to
  328. # get the History records of a User record.
  329. #
  330. # @parameter id(required) [Integer] The identifier matching the requested User record.
  331. #
  332. # @response_message 200 [History] The History records of the requested User record.
  333. # @response_message 401 Invalid session.
  334. def history
  335. # permission check
  336. if !role?(Z_ROLENAME_ADMIN) && !role?('Agent')
  337. response_access_deny
  338. return
  339. end
  340. # get user data
  341. user = User.find(params[:id])
  342. # get history of user
  343. history = user.history_get(true)
  344. # return result
  345. render json: history
  346. end
  347. =begin
  348. Resource:
  349. POST /api/v1/users/password_reset
  350. Payload:
  351. {
  352. "username": "some user name"
  353. }
  354. Response:
  355. {
  356. :message => 'ok'
  357. }
  358. Test:
  359. curl http://localhost/api/v1/users/password_reset.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"username": "some_username"}'
  360. =end
  361. def password_reset_send
  362. # check if feature is enabled
  363. if !Setting.get('user_lost_password')
  364. render json: { error: 'Feature not enabled!' }, status: :unprocessable_entity
  365. return
  366. end
  367. token = User.password_reset_send(params[:username])
  368. if token
  369. # only if system is in develop mode, send token back to browser for browser tests
  370. if Setting.get('developer_mode') == true
  371. render json: { message: 'ok', token: token.name }, status: :ok
  372. return
  373. end
  374. # token sent to user, send ok to browser
  375. render json: { message: 'ok' }, status: :ok
  376. return
  377. end
  378. # unable to generate token
  379. render json: { message: 'failed' }, status: :ok
  380. end
  381. =begin
  382. Resource:
  383. POST /api/v1/users/password_reset_verify
  384. Payload:
  385. {
  386. "token": "SoMeToKeN",
  387. "password": "new_password"
  388. }
  389. Response:
  390. {
  391. :message => 'ok'
  392. }
  393. Test:
  394. curl http://localhost/api/v1/users/password_reset_verify.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"token": "SoMeToKeN", "password" "new_password"}'
  395. =end
  396. def password_reset_verify
  397. if params[:password]
  398. # check password policy
  399. result = password_policy(params[:password])
  400. if result != true
  401. render json: { message: 'failed', notice: result }, status: :ok
  402. return
  403. end
  404. # set new password with token
  405. user = User.password_reset_via_token(params[:token], params[:password])
  406. else
  407. user = User.password_reset_check(params[:token])
  408. end
  409. if user
  410. render json: { message: 'ok', user_login: user.login }, status: :ok
  411. else
  412. render json: { message: 'failed' }, status: :ok
  413. end
  414. end
  415. =begin
  416. Resource:
  417. POST /api/v1/users/password_change
  418. Payload:
  419. {
  420. "password_old": "some_password_old",
  421. "password_new": "some_password_new"
  422. }
  423. Response:
  424. {
  425. :message => 'ok'
  426. }
  427. Test:
  428. curl http://localhost/api/v1/users/password_change.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"password_old": "password_old", "password_new": "password_new"}'
  429. =end
  430. def password_change
  431. # check old password
  432. if !params[:password_old]
  433. render json: { message: 'failed', notice: ['Current password needed!'] }, status: :ok
  434. return
  435. end
  436. user = User.authenticate( current_user.login, params[:password_old] )
  437. if !user
  438. render json: { message: 'failed', notice: ['Current password is wrong!'] }, status: :ok
  439. return
  440. end
  441. # set new password
  442. if !params[:password_new]
  443. render json: { message: 'failed', notice: ['Please supply your new password!'] }, status: :ok
  444. return
  445. end
  446. # check password policy
  447. result = password_policy(params[:password_new])
  448. if result != true
  449. render json: { message: 'failed', notice: result }, status: :ok
  450. return
  451. end
  452. user.update_attributes(password: params[:password_new])
  453. render json: { message: 'ok', user_login: user.login }, status: :ok
  454. end
  455. =begin
  456. Resource:
  457. PUT /api/v1/users/preferences.json
  458. Payload:
  459. {
  460. "language": "de",
  461. "notification": true
  462. }
  463. Response:
  464. {
  465. :message => 'ok'
  466. }
  467. Test:
  468. curl http://localhost/api/v1/users/preferences.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"language": "de", "notifications": true}'
  469. =end
  470. def preferences
  471. if !current_user
  472. render json: { message: 'No current user!' }, status: :unprocessable_entity
  473. return
  474. end
  475. if params[:user]
  476. user = User.find(current_user.id)
  477. params[:user].each {|key, value|
  478. user.preferences[key.to_sym] = value
  479. }
  480. user.save
  481. end
  482. render json: { message: 'ok' }, status: :ok
  483. end
  484. =begin
  485. Resource:
  486. DELETE /api/v1/users/account.json
  487. Payload:
  488. {
  489. "provider": "twitter",
  490. "uid": 581482342942
  491. }
  492. Response:
  493. {
  494. :message => 'ok'
  495. }
  496. Test:
  497. curl http://localhost/api/v1/users/account.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"provider": "twitter", "uid": 581482342942}'
  498. =end
  499. def account_remove
  500. if !current_user
  501. render json: { message: 'No current user!' }, status: :unprocessable_entity
  502. return
  503. end
  504. # provider + uid to remove
  505. if !params[:provider]
  506. render json: { message: 'provider needed!' }, status: :unprocessable_entity
  507. return
  508. end
  509. if !params[:uid]
  510. render json: { message: 'uid needed!' }, status: :unprocessable_entity
  511. return
  512. end
  513. # remove from database
  514. record = Authorization.where(
  515. user_id: current_user.id,
  516. provider: params[:provider],
  517. uid: params[:uid],
  518. )
  519. if !record.first
  520. render json: { message: 'No record found!' }, status: :unprocessable_entity
  521. return
  522. end
  523. record.destroy_all
  524. render json: { message: 'ok' }, status: :ok
  525. end
  526. =begin
  527. Resource:
  528. GET /api/v1/users/image/8d6cca1c6bdc226cf2ba131e264ca2c7
  529. Response:
  530. <IMAGE>
  531. Test:
  532. curl http://localhost/api/v1/users/image/8d6cca1c6bdc226cf2ba131e264ca2c7 -v -u #{login}:#{password}
  533. =end
  534. def image
  535. # cache image
  536. response.headers['Expires'] = 1.year.from_now.httpdate
  537. response.headers['Cache-Control'] = 'cache, store, max-age=31536000, must-revalidate'
  538. response.headers['Pragma'] = 'cache'
  539. file = Avatar.get_by_hash(params[:hash])
  540. if file
  541. send_data(
  542. file.content,
  543. filename: file.filename,
  544. type: file.preferences['Content-Type'] || file.preferences['Mime-Type'],
  545. disposition: 'inline'
  546. )
  547. return
  548. end
  549. # serve default image
  550. image = 'R0lGODdhMAAwAOMAAMzMzJaWlr6+vqqqqqOjo8XFxbe3t7GxsZycnAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAMAAwAAAEcxDISau9OOvNu/9gKI5kaZ5oqq5s675wLM90bd94ru98TwuAA+KQAQqJK8EAgBAgMEqmkzUgBIeSwWGZtR5XhSqAULACCoGCJGwlm1MGQrq9RqgB8fm4ZTUgDBIEcRR9fz6HiImKi4yNjo+QkZKTlJWWkBEAOw=='
  551. send_data(
  552. Base64.decode64(image),
  553. filename: 'image.gif',
  554. type: 'image/gif',
  555. disposition: 'inline'
  556. )
  557. end
  558. =begin
  559. Resource:
  560. POST /api/v1/users/avatar
  561. Payload:
  562. {
  563. "avatar_full": "base64 url",
  564. }
  565. Response:
  566. {
  567. message: 'ok'
  568. }
  569. Test:
  570. curl http://localhost/api/v1/users/avatar -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"avatar": "base64 url"}'
  571. =end
  572. def avatar_new
  573. return if !valid_session_with_user
  574. # get & validate image
  575. file_full = StaticAssets.data_url_attributes(params[:avatar_full])
  576. file_resize = StaticAssets.data_url_attributes(params[:avatar_resize])
  577. avatar = Avatar.add(
  578. object: 'User',
  579. o_id: current_user.id,
  580. full: {
  581. content: file_full[:content],
  582. mime_type: file_full[:mime_type],
  583. },
  584. resize: {
  585. content: file_resize[:content],
  586. mime_type: file_resize[:mime_type],
  587. },
  588. source: 'upload ' + Time.zone.now.to_s,
  589. deletable: true,
  590. )
  591. # update user link
  592. current_user.update_attributes(image: avatar.store_hash)
  593. render json: { avatar: avatar }, status: :ok
  594. end
  595. def avatar_set_default
  596. return if !valid_session_with_user
  597. # get & validate image
  598. if !params[:id]
  599. render json: { message: 'No id of avatar!' }, status: :unprocessable_entity
  600. return
  601. end
  602. # set as default
  603. avatar = Avatar.set_default('User', current_user.id, params[:id])
  604. # update user link
  605. current_user.update_attributes(image: avatar.store_hash)
  606. render json: {}, status: :ok
  607. end
  608. def avatar_destroy
  609. return if !valid_session_with_user
  610. # get & validate image
  611. if !params[:id]
  612. render json: { message: 'No id of avatar!' }, status: :unprocessable_entity
  613. return
  614. end
  615. # remove avatar
  616. Avatar.remove_one('User', current_user.id, params[:id])
  617. # update user link
  618. avatar = Avatar.get_default('User', current_user.id)
  619. current_user.update_attributes(image: avatar.store_hash)
  620. render json: {}, status: :ok
  621. end
  622. def avatar_list
  623. return if !valid_session_with_user
  624. # list of avatars
  625. result = Avatar.list('User', current_user.id)
  626. render json: { avatars: result }, status: :ok
  627. end
  628. private
  629. def password_policy(password)
  630. if Setting.get('password_min_size').to_i > password.length
  631. return ["Can\'t update password, it must be at least %s characters long!", Setting.get('password_min_size')]
  632. end
  633. if Setting.get('password_need_digit').to_i == 1 && password !~ /\d/
  634. return ["Can't update password, it must contain at least 1 digit!"]
  635. end
  636. if Setting.get('password_min_2_lower_2_upper_characters').to_i == 1 && ( password !~ /[A-Z].*[A-Z]/ || password !~ /[a-z].*[a-z]/ )
  637. return ["Can't update password, it must contain at least 2 lowercase and 2 uppercase characters!"]
  638. end
  639. true
  640. end
  641. def permission_check_by_role(params)
  642. return true if role?(Z_ROLENAME_ADMIN)
  643. if !role?('Admin') && params[:role_ids]
  644. if params[:role_ids].class != Array
  645. params[:role_ids] = [params[:role_ids]]
  646. end
  647. params[:role_ids].each {|role_id|
  648. role_local = Role.lookup(id: role_id)
  649. if !role_local
  650. render json: { error_human: 'Invalid role_ids!' }, status: :unauthorized
  651. logger.info "Invalid role_ids for current_user_id: #{current_user.id} role_ids #{role_id}"
  652. return false
  653. end
  654. role_name = role_local.name
  655. next if role_name != 'Admin' && role_name != 'Agent'
  656. render json: { error_human: 'This role assignment is only allowed by admin!' }, status: :unauthorized
  657. logger.info "This role assignment is only allowed by admin! current_user_id: #{current_user.id} assigned to #{role_name}"
  658. return false
  659. }
  660. end
  661. if role?('Agent') && params[:group_ids]
  662. if params[:group_ids].class != Array
  663. params[:group_ids] = [params[:group_ids]]
  664. end
  665. if !params[:group_ids].empty?
  666. render json: { error_human: 'Group relation is only allowed by admin!' }, status: :unauthorized
  667. logger.info "Group relation is only allowed by admin! current_user_id: #{current_user.id} group_ids #{params[:group_ids].inspect}"
  668. return false
  669. end
  670. end
  671. return true if role?('Agent')
  672. response_access_deny
  673. false
  674. end
  675. def permission_check
  676. return true if role?(Z_ROLENAME_ADMIN)
  677. return true if role?('Agent')
  678. # allow to update customer by him self
  679. return true if role?(Z_ROLENAME_CUSTOMER) && params[:id].to_i == current_user.id
  680. response_access_deny
  681. false
  682. end
  683. end