auth.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Return user as POJO
  64. getUserPOJO: function() {
  65. return {
  66. 'id': this.get('user.id'),
  67. 'username': this.get('user.username'),
  68. 'slug': this.get('user.slug'),
  69. 'avatar_hash': this.get('user.avatar_hash')
  70. };
  71. },
  72. // Anon/auth state
  73. isAnonymous: Ember.computed.not('isAuthenticated'),
  74. logout: function() {
  75. this.session.setItem('auth-user', false);
  76. this.session.setItem('auth-is-authenticated', false);
  77. Ember.$('#hidden-logout-form').submit();
  78. },
  79. // Utils for triggering 403 error
  80. _throw: function(message) {
  81. throw {
  82. status: 403,
  83. responseJSON: {
  84. detail: message
  85. }
  86. };
  87. },
  88. denyAuthenticated: function(message) {
  89. if (this.get('isAuthenticated')) {
  90. this._throw(message || gettext('This page is not available to signed in users.'));
  91. }
  92. },
  93. denyAnonymous: function(message) {
  94. if (this.get('isAnonymous')) {
  95. this._throw(message || gettext('This page is not available to guests.'));
  96. }
  97. }
  98. });