users_controller.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class UsersController < ApplicationController
  3. before_filter :authentication_check, :except => [:create, :password_reset_send, :password_reset_verify]
  4. =begin
  5. Format:
  6. JSON
  7. Example:
  8. {
  9. "id":2,
  10. "organization_id":null,
  11. "login":"m@edenhofer.de",
  12. "firstname":"Marti",
  13. "lastname":"Ede",
  14. "email":"m@edenhofer.de",
  15. "image_source":"http://www.gravatar.com/avatar/1c38b099f2344976005de69965733465?s=48",
  16. "web":"http://127.0.0.1",
  17. "password":"123",
  18. "phone":"112",
  19. "fax":"211",
  20. "mobile":"",
  21. "street":"",
  22. "zip":"",
  23. "city":"",
  24. "country":null,
  25. "verified":false,
  26. "active":true,
  27. "note":"some note",
  28. "source":null,
  29. "role_ids":[1,2],
  30. "group_ids":[1,2,3,4],
  31. }
  32. =end
  33. =begin
  34. Resource:
  35. GET /api/v1/users.json
  36. Response:
  37. [
  38. {
  39. "id": 1,
  40. "login": "some_login1",
  41. ...
  42. },
  43. {
  44. "id": 2,
  45. "login": "some_login2",
  46. ...
  47. }
  48. ]
  49. Test:
  50. curl http://localhost/api/v1/users.json -v -u #{login}:#{password}
  51. =end
  52. def index
  53. # only allow customer to fetch him self
  54. if is_role('Customer') && !is_role('Admin') && !is_role('Agent')
  55. users = User.where( :id => current_user.id )
  56. else
  57. users = User.all
  58. end
  59. users_all = []
  60. users.each {|user|
  61. users_all.push User.lookup( :id => user.id ).attributes_with_associations
  62. }
  63. render :json => users_all, :status => :ok
  64. end
  65. =begin
  66. Resource:
  67. GET /api/v1/users/1.json
  68. Response:
  69. {
  70. "id": 1,
  71. "login": "some_login1",
  72. ...
  73. },
  74. Test:
  75. curl http://localhost/api/v1/users/#{id}.json -v -u #{login}:#{password}
  76. =end
  77. def show
  78. # access deny
  79. if is_role('Customer') && !is_role('Admin') && !is_role('Agent')
  80. if params[:id].to_i != current_user.id
  81. response_access_deny
  82. return
  83. end
  84. end
  85. if params[:full]
  86. full = User.full( params[:id] )
  87. render :json => full
  88. return
  89. end
  90. user = User.find( params[:id] )
  91. render :json => user
  92. end
  93. =begin
  94. Resource:
  95. POST /api/v1/users.json
  96. Payload:
  97. {
  98. "login": "some_login",
  99. "firstname": "some firstname",
  100. "lastname": "some lastname",
  101. "email": "some@example.com"
  102. }
  103. Response:
  104. {
  105. "id": 1,
  106. "login": "some_login",
  107. ...
  108. },
  109. Test:
  110. curl http://localhost/api/v1/users.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"login": "some_login","firstname": "some firstname","lastname": "some lastname","email": "some@example.com"}'
  111. =end
  112. def create
  113. user = User.new( User.param_cleanup(params) )
  114. begin
  115. # check if it's first user
  116. count = User.all.count()
  117. # if it's a signup, add user to customer role
  118. if !current_user
  119. user.updated_by_id = 1
  120. user.created_by_id = 1
  121. # check if feature is enabled
  122. if !Setting.get('user_create_account')
  123. render :json => { :error => 'Feature not enabled!' }, :status => :unprocessable_entity
  124. return
  125. end
  126. # add first user as admin/agent and to all groups
  127. group_ids = []
  128. role_ids = []
  129. if count <= 2
  130. Role.where( :name => [ 'Admin', 'Agent'] ).each { |role|
  131. role_ids.push role.id
  132. }
  133. Group.all().each { |group|
  134. group_ids.push group.id
  135. }
  136. # everybody else will go as customer per default
  137. else
  138. role_ids.push Role.where( :name => 'Customer' ).first.id
  139. end
  140. user.role_ids = role_ids
  141. user.group_ids = group_ids
  142. # else do assignment as defined
  143. else
  144. if params[:role_ids]
  145. user.role_ids = params[:role_ids]
  146. end
  147. if params[:group_ids]
  148. user.group_ids = params[:group_ids]
  149. end
  150. end
  151. # check if user already exists
  152. if user.email
  153. exists = User.where( :email => user.email ).first
  154. if exists
  155. render :json => { :error => 'User already exists!' }, :status => :unprocessable_entity
  156. return
  157. end
  158. end
  159. user.save
  160. # if first user was added, set system init done
  161. if count <= 2
  162. Setting.set( 'system_init_done', true )
  163. end
  164. # send inviteation if needed / only if session exists
  165. if params[:invite] && current_user
  166. # generate token
  167. token = Token.create( :action => 'PasswordReset', :user_id => user.id )
  168. # send mail
  169. data = {}
  170. data[:subject] = 'Invitation to #{config.product_name} at #{config.fqdn}'
  171. data[:body] = 'Hi #{user.firstname},
  172. I (#{current_user.firstname} #{current_user.lastname}) invite you to #{config.product_name} - the customer support / ticket system platform.
  173. Click on the following link and set your password:
  174. #{config.http_type}://#{config.fqdn}/#password_reset_verify/#{token.name}
  175. Enjoy,
  176. #{current_user.firstname} #{current_user.lastname}
  177. Your #{config.product_name} Team
  178. '
  179. # prepare subject & body
  180. [:subject, :body].each { |key|
  181. data[key.to_sym] = NotificationFactory.build(
  182. :locale => user.locale,
  183. :string => data[key.to_sym],
  184. :objects => {
  185. :token => token,
  186. :user => user,
  187. :current_user => current_user,
  188. }
  189. )
  190. }
  191. # send notification
  192. NotificationFactory.send(
  193. :recipient => user,
  194. :subject => data[:subject],
  195. :body => data[:body]
  196. )
  197. end
  198. user_new = User.find( user.id )
  199. render :json => user_new, :status => :created
  200. rescue Exception => e
  201. render :json => { :error => e.message }, :status => :unprocessable_entity
  202. end
  203. end
  204. =begin
  205. Resource:
  206. PUT /api/v1/users/#{id}.json
  207. Payload:
  208. {
  209. "login": "some_login",
  210. "firstname": "some firstname",
  211. "lastname": "some lastname",
  212. "email": "some@example.com"
  213. }
  214. Response:
  215. {
  216. "id": 2,
  217. "login": "some_login",
  218. ...
  219. },
  220. Test:
  221. curl http://localhost/api/v1/users/2.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"login": "some_login","firstname": "some firstname","lastname": "some lastname","email": "some@example.com"}'
  222. =end
  223. def update
  224. # allow user to update him self
  225. if is_role('Customer') && !is_role('Admin') && !is_role('Agent')
  226. if params[:id] != current_user.id
  227. response_access_deny
  228. return
  229. end
  230. end
  231. user = User.find( params[:id] )
  232. begin
  233. user.update_attributes( User.param_cleanup(params) )
  234. # only allow Admin's and Agent's
  235. if is_role('Admin') && is_role('Agent') && params[:role_ids]
  236. user.role_ids = params[:role_ids]
  237. end
  238. # only allow Admin's
  239. if is_role('Admin') && params[:group_ids]
  240. user.group_ids = params[:group_ids]
  241. end
  242. # only allow Admin's and Agent's
  243. if is_role('Admin') && is_role('Agent') && params[:organization_ids]
  244. user.organization_ids = params[:organization_ids]
  245. end
  246. # get new data
  247. user_new = User.find( params[:id] )
  248. render :json => user_new, :status => :ok
  249. rescue Exception => e
  250. render :json => { :error => e.message }, :status => :unprocessable_entity
  251. end
  252. end
  253. # DELETE /api/v1/users/1
  254. def destroy
  255. return if deny_if_not_role('Admin')
  256. model_destory_render(User, params)
  257. end
  258. # GET /api/v1/users/search
  259. def search
  260. if is_role('Customer') && !is_role('Admin') && !is_role('Agent')
  261. response_access_deny
  262. return
  263. end
  264. query_params = {
  265. :query => params[:term],
  266. :limit => params[:limit],
  267. :current_user => current_user,
  268. }
  269. if params[:role_ids] && !params[:role_ids].empty?
  270. query_params[:role_ids] = params[:role_ids]
  271. end
  272. # do query
  273. user_all = User.search(query_params)
  274. # build result list
  275. if !params[:full]
  276. users = []
  277. user_all.each { |user|
  278. realname = user.firstname.to_s + ' ' + user.lastname.to_s
  279. if user.email && user.email.to_s != ''
  280. realname = realname + ' <' + user.email.to_s + '>'
  281. end
  282. a = { :id => user.id, :label => realname, :value => realname }
  283. users.push a
  284. }
  285. # return result
  286. render :json => users
  287. return
  288. end
  289. user_ids = []
  290. assets = {}
  291. user_all.each { |user|
  292. assets = user.assets(assets)
  293. user_ids.push user.id
  294. }
  295. # return result
  296. render :json => {
  297. :assets => assets,
  298. :user_ids => user_ids.uniq,
  299. }
  300. end
  301. # GET /api/v1/users/history/1
  302. def history
  303. # permissin check
  304. if !is_role('Admin') && !is_role('Agent')
  305. response_access_deny
  306. return
  307. end
  308. # get user data
  309. user = User.find( params[:id] )
  310. # get history of user
  311. history = user.history_get(true)
  312. # return result
  313. render :json => history
  314. end
  315. =begin
  316. Resource:
  317. POST /api/v1/users/password_reset
  318. Payload:
  319. {
  320. "username": "some user name"
  321. }
  322. Response:
  323. {
  324. :message => 'ok'
  325. }
  326. Test:
  327. curl http://localhost/api/v1/users/password_reset.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"username": "some_username"}'
  328. =end
  329. def password_reset_send
  330. # check if feature is enabled
  331. if !Setting.get('user_lost_password')
  332. render :json => { :error => 'Feature not enabled!' }, :status => :unprocessable_entity
  333. return
  334. end
  335. success = User.password_reset_send( params[:username] )
  336. if success
  337. render :json => { :message => 'ok' }, :status => :ok
  338. else
  339. render :json => { :message => 'failed' }, :status => :unprocessable_entity
  340. end
  341. end
  342. =begin
  343. Resource:
  344. POST /api/v1/users/password_reset_verify
  345. Payload:
  346. {
  347. "token": "SoMeToKeN",
  348. "password" "new_password"
  349. }
  350. Response:
  351. {
  352. :message => 'ok'
  353. }
  354. Test:
  355. 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"}'
  356. =end
  357. def password_reset_verify
  358. if params[:password]
  359. user = User.password_reset_via_token( params[:token], params[:password] )
  360. else
  361. user = User.password_reset_check( params[:token] )
  362. end
  363. if user
  364. render :json => { :message => 'ok', :user_login => user.login }, :status => :ok
  365. else
  366. render :json => { :message => 'failed' }, :status => :unprocessable_entity
  367. end
  368. end
  369. =begin
  370. Resource:
  371. POST /api/v1/users/password_change
  372. Payload:
  373. {
  374. "password_old": "some_password_old",
  375. "password_new": "some_password_new"
  376. }
  377. Response:
  378. {
  379. :message => 'ok'
  380. }
  381. Test:
  382. 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"}'
  383. =end
  384. def password_change
  385. # check old password
  386. if !params[:password_old]
  387. render :json => { :message => 'Old password needed!' }, :status => :unprocessable_entity
  388. return
  389. end
  390. user = User.authenticate( current_user.login, params[:password_old] )
  391. if !user
  392. render :json => { :message => 'Old password is wrong!' }, :status => :unprocessable_entity
  393. return
  394. end
  395. # set new password
  396. if !params[:password_new]
  397. render :json => { :message => 'New password needed!' }, :status => :unprocessable_entity
  398. return
  399. end
  400. user.update_attributes( :password => params[:password_new] )
  401. render :json => { :message => 'ok', :user_login => user.login }, :status => :ok
  402. end
  403. =begin
  404. Resource:
  405. PUT /api/v1/users/preferences.json
  406. Payload:
  407. {
  408. "language": "de",
  409. "notification": true
  410. }
  411. Response:
  412. {
  413. :message => 'ok'
  414. }
  415. Test:
  416. curl http://localhost/api/v1/users/preferences.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"language": "de", "notifications": true}'
  417. =end
  418. def preferences
  419. if !current_user
  420. render :json => { :message => 'No current user!' }, :status => :unprocessable_entity
  421. return
  422. end
  423. if params[:user]
  424. params[:user].each {|key, value|
  425. current_user.preferences[key.to_sym] = value
  426. }
  427. end
  428. current_user.save
  429. render :json => { :message => 'ok' }, :status => :ok
  430. end
  431. =begin
  432. Resource:
  433. DELETE /api/v1/users/account.json
  434. Payload:
  435. {
  436. "provider": "twitter",
  437. "uid": 581482342942
  438. }
  439. Response:
  440. {
  441. :message => 'ok'
  442. }
  443. Test:
  444. curl http://localhost/api/v1/users/account.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"provider": "twitter", "uid": 581482342942}'
  445. =end
  446. def account_remove
  447. if !current_user
  448. render :json => { :message => 'No current user!' }, :status => :unprocessable_entity
  449. return
  450. end
  451. # provider + uid to remove
  452. if !params[:provider]
  453. render :json => { :message => 'provider needed!' }, :status => :unprocessable_entity
  454. return
  455. end
  456. if !params[:uid]
  457. render :json => { :message => 'uid needed!' }, :status => :unprocessable_entity
  458. return
  459. end
  460. # remove from database
  461. record = Authorization.where(
  462. :user_id => current_user.id,
  463. :provider => params[:provider],
  464. :uid => params[:uid],
  465. )
  466. if !record.first
  467. render :json => { :message => 'No record found!' }, :status => :unprocessable_entity
  468. return
  469. end
  470. record.destroy_all
  471. render :json => { :message => 'ok' }, :status => :ok
  472. end
  473. =begin
  474. Resource:
  475. GET /api/v1/users/image/8d6cca1c6bdc226cf2ba131e264ca2c7
  476. Response:
  477. <IMAGE>
  478. Test:
  479. curl http://localhost/api/v1/users/image/8d6cca1c6bdc226cf2ba131e264ca2c7 -v -u #{login}:#{password}
  480. =end
  481. def image
  482. # cache image
  483. response.headers['Expires'] = 1.year.from_now.httpdate
  484. response.headers["Cache-Control"] = "cache, store, max-age=31536000, must-revalidate"
  485. response.headers["Pragma"] = "cache"
  486. user = User.where( :image => params[:hash] ).first
  487. if user
  488. image = user.get_image
  489. send_data(
  490. image[:content],
  491. :filename => image[:filename],
  492. :type => image[:content_type],
  493. :disposition => 'inline'
  494. )
  495. return
  496. end
  497. render :json => {}, :status => 404
  498. end
  499. end