users_controller.rb 23 KB

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