|
1 | 1 | (function () { |
2 | | - 'use strict'; |
3 | | - |
4 | | - angular.module('frontend.core.auth.services') |
5 | | - .factory('AuthService', [ |
6 | | - '$http', '$state', '$localStorage', '$rootScope', |
7 | | - 'AccessLevels', 'BackendConfig', 'MessageService', |
8 | | - function factory($http, $state, $localStorage, $rootScope, |
9 | | - AccessLevels, BackendConfig, MessageService) { |
10 | | - return { |
11 | | - /** |
12 | | - * Method to authorize current user with given access level in application. |
13 | | - * |
14 | | - * @param {Number} accessLevel Access level to check |
15 | | - * |
16 | | - * @returns {Boolean} |
17 | | - */ |
18 | | - authorize: function authorize(accessLevel) { |
19 | | - |
20 | | - |
21 | | - if (accessLevel === AccessLevels.user) { |
22 | | - return this.isAuthenticated(); |
23 | | - } else if (accessLevel === AccessLevels.admin) { |
24 | | - return this.isAuthenticated() && Boolean($localStorage.credentials.user.admin); |
25 | | - } else { |
26 | | - return accessLevel === AccessLevels.anon; |
27 | | - } |
28 | | - }, |
29 | | - |
30 | | - hasPermission: function (context, action) { |
31 | | - |
32 | | - // If user is admin or context is not a permissions Object key, grant permission |
33 | | - if (($localStorage.credentials && $localStorage.credentials.user.admin) |
34 | | - || Object.keys(KONGA_CONFIG.user_permissions).indexOf(context) < 0) { |
35 | | - return true; |
36 | | - } |
37 | | - |
38 | | - action = action || 'read'; // Default action is 'read' |
39 | | - |
40 | | - /** |
41 | | - * ====================================================================================== |
42 | | - * Monkey patches. |
43 | | - * ====================================================================================== |
44 | | - */ |
45 | | - |
46 | | - // Transform 'edit' action to 'update' |
47 | | - // because permissions object complies to CRUD naming. |
48 | | - // ToDo : Change 'edit' route uri segments to 'update' |
49 | | - if(action === 'edit') { |
50 | | - action = 'update'; |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * ====================================================================================== |
55 | | - * End monkey patches. |
56 | | - * ====================================================================================== |
57 | | - */ |
58 | | - |
59 | | - return KONGA_CONFIG.user_permissions[context] |
60 | | - && KONGA_CONFIG.user_permissions[context][action] === true |
61 | | - |
62 | | - }, |
63 | | - |
64 | | - /** |
65 | | - * Method to check if current user is authenticated or not. This will just |
66 | | - * simply call 'Storage' service 'get' method and returns it results. |
67 | | - * |
68 | | - * @returns {Boolean} |
69 | | - */ |
70 | | - isAuthenticated: function isAuthenticated() { |
71 | | - return Boolean($localStorage.credentials); |
72 | | - }, |
73 | | - |
74 | | - |
75 | | - /** |
76 | | - * Method to check if current user is an admin or not. |
77 | | - * |
78 | | - * @returns {Boolean} |
79 | | - */ |
80 | | - isAdmin : function isAdmin() { |
81 | | - |
82 | | - return $localStorage.credentials && $localStorage.credentials.user && $localStorage.credentials.user.admin; |
83 | | - |
84 | | - }, |
85 | | - |
86 | | - |
87 | | - token: function token() { |
88 | | - return $localStorage.credentials ? $localStorage.credentials.token : null; |
89 | | - }, |
90 | | - |
91 | | - /** |
92 | | - * Method make login request to backend server. Successfully response from |
93 | | - * server contains user data and JWT token as in JSON object. After successful |
94 | | - * authentication method will store user data and JWT token to local storage |
95 | | - * where those can be used. |
96 | | - * |
97 | | - * @param {*} credentials |
98 | | - * |
99 | | - * @returns {*|Promise} |
100 | | - */ |
101 | | - login: function login(credentials) { |
102 | | - return $http |
103 | | - .post('login', credentials, {withCredentials: true}) |
104 | | - .then( |
105 | | - function (response) { |
106 | | - MessageService.success('You have logged in successfully!'); |
107 | | - $localStorage.credentials = response.data; |
108 | | - $rootScope.$broadcast('user.login', $localStorage.credentials) |
109 | | - } |
110 | | - ) |
111 | | - ; |
112 | | - }, |
113 | | - |
114 | | - /** |
115 | | - * The backend doesn't care about actual user logout, just delete the token |
116 | | - * and you're good to go. |
117 | | - * |
118 | | - * Question still: Should we make logout process to backend side? |
119 | | - */ |
120 | | - logout: function logout() { |
121 | | - $localStorage.$reset(); |
122 | | - |
123 | | - MessageService.success('You have logged out.'); |
124 | | - |
125 | | - $state.go('auth.login'); |
126 | | - } |
127 | | - }; |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + angular.module('frontend.core.auth.services') |
| 5 | + .factory('AuthService', [ |
| 6 | + '$http', '$state', '$localStorage', '$rootScope', |
| 7 | + 'AccessLevels', 'BackendConfig', 'MessageService', |
| 8 | + function factory($http, $state, $localStorage, $rootScope, |
| 9 | + AccessLevels, BackendConfig, MessageService) { |
| 10 | + return { |
| 11 | + /** |
| 12 | + * Method to authorize current user with given access level in application. |
| 13 | + * |
| 14 | + * @param {Number} accessLevel Access level to check |
| 15 | + * |
| 16 | + * @returns {Boolean} |
| 17 | + */ |
| 18 | + authorize: function authorize(accessLevel) { |
| 19 | + |
| 20 | + |
| 21 | + if (accessLevel === AccessLevels.user) { |
| 22 | + return this.isAuthenticated(); |
| 23 | + } else if (accessLevel === AccessLevels.admin) { |
| 24 | + return this.isAuthenticated() && Boolean($localStorage.credentials.user.admin); |
| 25 | + } else { |
| 26 | + return accessLevel === AccessLevels.anon; |
128 | 27 | } |
129 | | - ]) |
130 | | - ; |
| 28 | + }, |
| 29 | + |
| 30 | + hasPermission: function (context, action) { |
| 31 | + |
| 32 | + // If user is admin or context is not a permissions Object key, grant permission |
| 33 | + if (($localStorage.credentials && $localStorage.credentials.user.admin) |
| 34 | + || Object.keys(KONGA_CONFIG.user_permissions).indexOf(context) < 0) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + action = action || 'read'; // Default action is 'read' |
| 39 | + |
| 40 | + /** |
| 41 | + * ====================================================================================== |
| 42 | + * Monkey patches. |
| 43 | + * ====================================================================================== |
| 44 | + */ |
| 45 | + |
| 46 | + // Transform 'edit' action to 'update' |
| 47 | + // because permissions object complies to CRUD naming. |
| 48 | + // ToDo : Change 'edit' route uri segments to 'update' |
| 49 | + if (action === 'edit') { |
| 50 | + action = 'update'; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * ====================================================================================== |
| 55 | + * End monkey patches. |
| 56 | + * ====================================================================================== |
| 57 | + */ |
| 58 | + |
| 59 | + return KONGA_CONFIG.user_permissions[context] |
| 60 | + && KONGA_CONFIG.user_permissions[context][action] === true |
| 61 | + |
| 62 | + }, |
| 63 | + |
| 64 | + /** |
| 65 | + * Method to check if current user is authenticated or not. This will just |
| 66 | + * simply call 'Storage' service 'get' method and returns it results. |
| 67 | + * |
| 68 | + * @returns {Boolean} |
| 69 | + */ |
| 70 | + isAuthenticated: function isAuthenticated() { |
| 71 | + return Boolean($localStorage.credentials); |
| 72 | + }, |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Method to check if current user is an admin or not. |
| 77 | + * |
| 78 | + * @returns {Boolean} |
| 79 | + */ |
| 80 | + isAdmin: function isAdmin() { |
| 81 | + |
| 82 | + return $localStorage.credentials && $localStorage.credentials.user && $localStorage.credentials.user.admin; |
| 83 | + |
| 84 | + }, |
| 85 | + |
| 86 | + |
| 87 | + token: function token() { |
| 88 | + return $localStorage.credentials ? $localStorage.credentials.token : null; |
| 89 | + }, |
| 90 | + |
| 91 | + /** |
| 92 | + * Method make login request to backend server. Successfully response from |
| 93 | + * server contains user data and JWT token as in JSON object. After successful |
| 94 | + * authentication method will store user data and JWT token to local storage |
| 95 | + * where those can be used. |
| 96 | + * |
| 97 | + * @param {*} credentials |
| 98 | + * |
| 99 | + * @returns {*|Promise} |
| 100 | + */ |
| 101 | + login: function login(credentials) { |
| 102 | + return $http |
| 103 | + .post('login', credentials, {withCredentials: true}) |
| 104 | + .then( |
| 105 | + function (response) { |
| 106 | + MessageService.success('You have logged in successfully!'); |
| 107 | + $localStorage.credentials = response.data; |
| 108 | + $rootScope.$broadcast('user.login', $localStorage.credentials) |
| 109 | + $rootScope.user = response.data.user; |
| 110 | + } |
| 111 | + ) |
| 112 | + ; |
| 113 | + }, |
| 114 | + |
| 115 | + /** |
| 116 | + * The backend doesn't care about actual user logout, just delete the token |
| 117 | + * and you're good to go. |
| 118 | + * |
| 119 | + * Question still: Should we make logout process to backend side? |
| 120 | + */ |
| 121 | + logout: function logout() { |
| 122 | + $localStorage.$reset(); |
| 123 | + MessageService.success('You have logged out.'); |
| 124 | + $rootScope.user = null; |
| 125 | + $state.go('auth.login'); |
| 126 | + } |
| 127 | + }; |
| 128 | + } |
| 129 | + ]) |
| 130 | + ; |
131 | 131 | }()); |
0 commit comments