11import router from 'ROUTER'
22import store from 'STORE'
3+ import loginTypes from 'STORE/modules/login/mutations/types'
34import NProgress from 'nprogress'
45import 'nprogress/nprogress.css'
56import { getTokenFromLocal } from 'UTILS/storage'
67import { MessageBox } from 'element-ui'
8+ import dynamicRoutes from 'ROUTER/routes/dynamic'
9+ import constantRoutes from 'ROUTER/routes/constant'
710
811NProgress . configure ( { showSpinner : false } )
912
@@ -28,13 +31,22 @@ router.beforeEach((to, from, next) => {
2831 if ( getTokenFromLocal ( ) ) {
2932 // 1. fetch rolesMap if necessary (when rolesMap stored by back-end)
3033
31- // 2. confirm route access by user role
34+ // 2. confirm route access by user role, including global routes creation.
3235 if ( ! store . getters [ 'login/role' ] ) {
3336 // 2.1 No roles: fetch user role
3437 return store . dispatch (
3538 'login/fetchUserAccess' ,
3639 store . getters [ 'login/username' ]
3740 )
41+ . then ( setDynamicRoutesToStorage )
42+ . then ( setGlobalRoutesToStorage )
43+ . then ( ( ) => {
44+ router . addRoutes ( store . getters [ 'login/dynamicRoutes' ] )
45+ console . log (
46+ '[Routes creation]: routes has been added!' ,
47+ store . getters [ 'login/dynamicRoutes' ]
48+ )
49+ } )
3850 . catch ( e => errorHandler ( e , next , to . path ) )
3951 . finally ( ( ) => next ( ) )
4052 }
@@ -79,3 +91,59 @@ function errorHandler (e, next, redirectPath) {
7991 NProgress . done ( )
8092 console . error ( e )
8193}
94+
95+ /**
96+ * @param {String } role User access
97+ */
98+ function createDynamicRoutes ( role ) {
99+ if ( typeof role !== 'string' ) throw new Error ( '[Dynamic routes]: Wrong role.' )
100+
101+ return filterRoutes ( dynamicRoutes , role )
102+ // ! ADMINISTRATOR has all route access if necessary.
103+ // return role === ADMINISTRATOR
104+ // ? dynamicRoutes
105+ // : filterRoutes(dynamicRoutes, role)
106+ }
107+
108+ /**
109+ * @param {Object[] } routes Original routes
110+ * @param {String } role User access
111+ */
112+ function filterRoutes ( routes , role ) {
113+ return routes . reduce ( ( accumulator , route ) => {
114+ const routeCopy = { ...route } // shallow copy
115+ if ( hasAccess ( route , role ) ) {
116+ if ( routeCopy . children ) {
117+ // filter original children routes, **override** all children routes.
118+ routeCopy . children = filterRoutes ( routeCopy . children , role )
119+ }
120+
121+ // Skip `push` operation if all the route children has been filtered.
122+ if ( ! ( routeCopy . children && ! routeCopy . children . length ) ) {
123+ accumulator . push ( routeCopy )
124+ }
125+ }
126+ return accumulator
127+ } , [ ] )
128+ }
129+
130+ function hasAccess ( route , role ) {
131+ return route . meta && route . meta . roles
132+ ? route . meta . roles . includes ( role )
133+ : true
134+ }
135+
136+ function setDynamicRoutesToStorage ( roles ) {
137+ const currentRole = Array . isArray ( roles ) ? roles [ 0 ] : roles
138+ store . commit (
139+ `login/${ loginTypes . SET_DYNAMIC_ROUTES } ` ,
140+ createDynamicRoutes ( currentRole )
141+ )
142+ }
143+
144+ function setGlobalRoutesToStorage ( ) {
145+ store . commit ( `login/${ loginTypes . SET_ALL_ROUTES } ` , [
146+ ...constantRoutes ,
147+ ...store . getters [ 'login/dynamicRoutes' ]
148+ ] )
149+ }
0 commit comments