auth.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Ember from 'ember';
  2. export default Ember.Service.extend({
  3. // State synchronization across tabs
  4. needsSync: false, // becomes true if auth state between tabs differs
  5. syncToUser: null, // becomes user obj to which we want to sync or null for anon
  6. syncSession: function() {
  7. this.session.setItem('auth-user', this.get('user'));
  8. this.session.setItem('auth-is-authenticated', this.get('isAuthenticated'));
  9. var self = this;
  10. this.session.watchItem('auth-is-authenticated', function(isAuthenticated) {
  11. self._handleAuthChange(isAuthenticated);
  12. });
  13. this.session.watchItem('auth-user', function(newUser) {
  14. self._handleUserChange(newUser);
  15. });
  16. }.on('init'),
  17. _handleAuthChange: function(isAuthenticated) {
  18. if (!this.get('needsSync')) {
  19. // display annoying "you were desynced" message
  20. this.set('needsSync', true);
  21. if (isAuthenticated) {
  22. this.set('syncToUser', Ember.Object.create(this.session.getItem('auth-user')));
  23. }
  24. }
  25. },
  26. _handleUserChange: function(newUser) {
  27. if (!this.get('needsSync')) {
  28. var userObj = Ember.Object.create(newUser);
  29. if (userObj.get('id') !== this.get('user.id')) {
  30. this.setProperties({
  31. 'needsSync': true,
  32. 'syncToUser': userObj,
  33. });
  34. } else {
  35. this.get('user').setProperties(newUser);
  36. }
  37. }
  38. },
  39. userObserver: function() {
  40. this.session.setItem('auth-user', this.get('user'));
  41. }.observes('user.username',
  42. 'user.slug',
  43. 'user.email',
  44. 'user.is_hiding_presence',
  45. 'user.avatar_hash',
  46. 'user.new_notifications',
  47. 'user.limits_private_thread_invites_to',
  48. 'user.unread_private_threads',
  49. 'user.subscribe_to_started_threads',
  50. 'user.subscribe_to_replied_threads'),
  51. // Return user as POJO
  52. getUserPOJO: function() {
  53. return {
  54. 'id': this.get('user.id'),
  55. 'username': this.get('user.username'),
  56. 'slug': this.get('user.slug'),
  57. 'avatar_hash': this.get('user.avatar_hash')
  58. };
  59. },
  60. // Anon/auth state
  61. isAnonymous: Ember.computed.not('isAuthenticated'),
  62. logout: function() {
  63. this.session.setItem('auth-user', false);
  64. this.session.setItem('auth-is-authenticated', false);
  65. Ember.$('#hidden-logout-form').submit();
  66. },
  67. // Utils for triggering 403 error
  68. _throw: function(message) {
  69. throw {
  70. status: 403,
  71. responseJSON: {
  72. detail: message
  73. }
  74. };
  75. },
  76. denyAuthenticated: function(message) {
  77. if (this.get('isAuthenticated')) {
  78. this._throw(message || gettext('This page is not available to signed in users.'));
  79. }
  80. },
  81. denyAnonymous: function(message) {
  82. if (this.get('isAnonymous')) {
  83. this._throw(message || gettext('This page is not available to guests.'));
  84. }
  85. }
  86. });