auth.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. setUrlNameOnUser: function() {
  18. this.get('user').reopen({
  19. url_name: function() {
  20. return this.get('slug') + '-' + this.get('id');
  21. }.property('id', 'slug')
  22. });
  23. }.on('init'),
  24. _handleAuthChange: function(isAuthenticated) {
  25. if (!this.get('needsSync')) {
  26. // display annoying "you were desynced" message
  27. this.set('needsSync', true);
  28. if (isAuthenticated) {
  29. this.set('syncToUser', Ember.Object.create(this.session.getItem('auth-user')));
  30. }
  31. }
  32. },
  33. _handleUserChange: function(newUser) {
  34. if (!this.get('needsSync')) {
  35. var userObj = Ember.Object.create(newUser);
  36. if (userObj.get('id') !== this.get('user.id')) {
  37. this.setProperties({
  38. 'needsSync': true,
  39. 'syncToUser': userObj,
  40. });
  41. } else {
  42. this.get('user').setProperties(newUser);
  43. }
  44. }
  45. },
  46. userObserver: function() {
  47. this.session.setItem('auth-user', this.get('user'));
  48. }.observes('user.username',
  49. 'user.slug',
  50. 'user.email',
  51. 'user.is_hiding_presence',
  52. 'user.avatar_hash',
  53. 'user.new_notifications',
  54. 'user.limits_private_thread_invites_to',
  55. 'user.unread_private_threads',
  56. 'user.subscribe_to_started_threads',
  57. 'user.subscribe_to_replied_threads'),
  58. // Return user as POJO
  59. getUserPOJO: function() {
  60. return {
  61. 'id': this.get('user.id'),
  62. 'username': this.get('user.username'),
  63. 'slug': this.get('user.slug'),
  64. 'avatar_hash': this.get('user.avatar_hash')
  65. };
  66. },
  67. // Anon/auth state
  68. isAnonymous: Ember.computed.not('isAuthenticated'),
  69. logout: function() {
  70. this.session.setItem('auth-user', false);
  71. this.session.setItem('auth-is-authenticated', false);
  72. Ember.$('#hidden-logout-form').submit();
  73. },
  74. // Utils for triggering 403 error
  75. _throw: function(message) {
  76. throw {
  77. status: 403,
  78. responseJSON: {
  79. detail: message
  80. }
  81. };
  82. },
  83. denyAuthenticated: function(message) {
  84. if (this.get('isAuthenticated')) {
  85. this._throw(message || gettext('This page is not available to signed in users.'));
  86. }
  87. },
  88. denyAnonymous: function(message) {
  89. if (this.get('isAnonymous')) {
  90. this._throw(message || gettext('This page is not available to guests.'));
  91. }
  92. }
  93. });