-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathpermission-ui.js
152 lines (132 loc) · 4.46 KB
/
permission-ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
/**
* @namespace permission.ui
*/
/**
* @param $stateProvider {Object}
*/
function config($stateProvider) {
'ngInject';
$stateProvider.decorator('$state', function (state) {
/**
* Property containing full state object definition
*
* This decorator is required to access full state object instead of just it's configuration
* Can be removed when implemented https://github.com/angular-ui/ui-router/issues/13.
*
* @returns {Object}
*/
state.self.$$permissionState = function () {
return state;
};
return state;
});
}
/**
* @param $rootScope {Object}
* @param $state {Object}
* @param PermTransitionProperties {permission.PermTransitionProperties}
* @param PermTransitionEvents {permission.ui.PermTransitionEvents}
* @param PermStateAuthorization {permission.ui.PermStateAuthorization}
* @param PermStatePermissionMap {permission.ui.PermStatePermissionMap}
*/
function run($rootScope, $state, PermTransitionProperties, PermTransitionEvents, PermStateAuthorization, PermStatePermissionMap) {
'ngInject';
/**
* State transition interceptor
*/
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, options) {
if (!isAuthorizationFinished()) {
setStateAuthorizationStatus(true);
setTransitionProperties();
if (!PermTransitionEvents.areEventsDefaultPrevented()) {
PermTransitionEvents.broadcastPermissionStartEvent();
event.preventDefault();
var statePermissionMap = new PermStatePermissionMap(PermTransitionProperties.toState);
PermStateAuthorization
.authorizeByPermissionMap(statePermissionMap)
.then(function () {
handleAuthorizedState();
})
.catch(function (rejectedPermission) {
handleUnauthorizedState(rejectedPermission, statePermissionMap);
})
.finally(function () {
setStateAuthorizationStatus(false);
});
} else {
setStateAuthorizationStatus(false);
}
}
/**
* Updates values of `PermTransitionProperties` holder object
* @method
* @private
*/
function setTransitionProperties() {
PermTransitionProperties.toState = toState;
PermTransitionProperties.toParams = toParams;
PermTransitionProperties.fromState = fromState;
PermTransitionProperties.fromParams = fromParams;
PermTransitionProperties.options = options;
}
/**
* Sets internal state `$$finishedAuthorization` variable to prevent looping
* @method
* @private
*
* @param status {boolean} When true authorization has been already preceded
*/
function setStateAuthorizationStatus(status) {
angular.extend(toState, {'$$isAuthorizationFinished': status});
}
/**
* Checks if state has been already checked for authorization
* @method
* @private
*
* @returns {boolean}
*/
function isAuthorizationFinished() {
return toState.$$isAuthorizationFinished;
}
/**
* Handles redirection for authorized access
* @method
* @private
*/
function handleAuthorizedState() {
PermTransitionEvents.broadcastPermissionAcceptedEvent();
// Overwrite notify option to broadcast it later
var transitionOptions = angular.extend({}, PermTransitionProperties.options, {notify: false, location: true});
$state
.go(PermTransitionProperties.toState.name, PermTransitionProperties.toParams, transitionOptions)
.then(function () {
PermTransitionEvents.broadcastStateChangeSuccessEvent();
});
}
/**
* Handles redirection for unauthorized access
* @method
* @private
*
* @param rejectedPermission {String} Rejected access right
* @param statePermissionMap {permission.ui.PermPermissionMap} State permission map
*/
function handleUnauthorizedState(rejectedPermission, statePermissionMap) {
PermTransitionEvents.broadcastPermissionDeniedEvent();
statePermissionMap
.resolveRedirectState(rejectedPermission)
.then(function (redirect) {
$state.go(redirect.state, redirect.params, redirect.options);
});
}
});
}
var uiPermission = angular
.module('permission.ui', ['permission', 'ui.router'])
.config(config)
.run(run);
if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.exports === exports) {
module.exports = uiPermission.name;
}