auth.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // User url name
  52. setUrlNameOnUser: function() {
  53. if (this.get('isAuthenticated')) {
  54. this.get('user').set('url_name', this.get('user.slug') + '-' + this.get('user.id'));
  55. }
  56. },
  57. setUserUrlNameOnInit: function() {
  58. this.setUrlNameOnUser();
  59. }.on('init'),
  60. syncUrlNameOnUser: function() {
  61. this.setUrlNameOnUser();
  62. }.observes('user.id', 'user.slug'),
  63. // Propagate changes to store for nice real-time changes
  64. propagateAvatarChange: function() {
  65. var user = this.store.getById('user', this.get('user.id'));
  66. if (user) {
  67. user.set('avatar_hash', this.get('user.avatar_hash'));
  68. }
  69. var profile = this.store.getById('user-profile', this.get('user.id'));
  70. if (profile) {
  71. profile.set('avatar_hash', this.get('user.avatar_hash'));
  72. }
  73. }.observes('user.avatar_hash'),
  74. // Return user as POJO
  75. getUserPOJO: function() {
  76. return {
  77. 'id': this.get('user.id'),
  78. 'username': this.get('user.username'),
  79. 'slug': this.get('user.slug'),
  80. 'avatar_hash': this.get('user.avatar_hash')
  81. };
  82. },
  83. // Anon/auth state
  84. isAnonymous: Ember.computed.not('isAuthenticated'),
  85. logout: function() {
  86. this.session.setItem('auth-user', false);
  87. this.session.setItem('auth-is-authenticated', false);
  88. Ember.$('#hidden-logout-form').submit();
  89. },
  90. // Utils for triggering 403 error
  91. _throw: function(message) {
  92. throw {
  93. status: 403,
  94. responseJSON: {
  95. detail: message
  96. }
  97. };
  98. },
  99. denyAuthenticated: function(message) {
  100. if (this.get('isAuthenticated')) {
  101. this._throw(message || gettext('This page is not available to signed in users.'));
  102. }
  103. },
  104. denyAnonymous: function(message) {
  105. if (this.get('isAnonymous')) {
  106. this._throw(message || gettext('This page is not available to guests.'));
  107. }
  108. }
  109. });