Skip to content

Commit f3b47ba

Browse files
committed
Attaching roles and abilities on Login Loader
1 parent e433349 commit f3b47ba

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

angular/app/components/login-loader/login-loader.component.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
class LoginLoaderController {
2-
constructor ($state, $auth, API) {
2+
constructor ($state, $auth, API, AclService) {
33
'ngInject'
44

55
API.oneUrl('authenticate').one('user').get().then((response) => {
66
if (!response.error) {
7-
$auth.setToken(response.data)
7+
let data = response.data
8+
9+
angular.forEach(data.userRole, function (value, key) {
10+
AclService.attachRole(value)
11+
})
12+
13+
AclService.setAbilities(data.abilities)
14+
$auth.setToken(data.token)
815
$state.go('app.landing')
916
}
1017
})

angular/config/routes.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export function RoutesConfig ($stateProvider, $urlRouterProvider) {
166166
.state('loginloader', {
167167
url: '/login-loader',
168168
views: {
169-
'main@app': {
169+
'layout': {
170170
templateUrl: getView('login-loader')
171171
},
172172
'header@app': {},
@@ -207,6 +207,7 @@ export function RoutesConfig ($stateProvider, $urlRouterProvider) {
207207
controller: function ($scope, $auth, $state, AclService) {
208208
$auth.logout().then(function (oldUser) {
209209
AclService.flushRoles()
210+
AclService.setAbilities({})
210211
$state.go('login')
211212
})
212213
}

app/Http/Controllers/Auth/AuthController.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ public function getAuthenticatedUser()
1717
$user = Auth::user();
1818
$token = JWTAuth::fromUser($user);
1919

20-
return response()->success($token);
20+
$abilities = array(
21+
'guest' => array('login'),
22+
'user' => array('logout', 'view_content'),
23+
'admin' => array('logout', 'manage_content', 'view_content'),
24+
);
25+
26+
$userRole = array('admin', 'user');
27+
28+
return response()->success(compact('user', 'token', 'abilities', 'userRole'));
2129
} else {
2230
return response()->error('unauthorized', 401);
2331
}

app/Http/routes.php

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
});
3030

3131
// ADMIN ROUTES
32+
33+
$api->group(['middleware' => ['api', 'api.auth']], function ($api) {
34+
$api->controller('users', 'UserController');
35+
});
36+
37+
/* EXAMPLE PROTECTED ROUTE
38+
3239
$api->group(['middleware' => ['api', 'api.auth', 'role:admin']], function ($api) {
3340
$api->controller('users', 'UserController');
3441
});
42+
43+
*/

app/User.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
2222
* @var array
2323
*/
2424
protected $fillable = [
25-
'name', 'email', 'password', 'avatar','oauth_provider_id', 'oauth_provider'
25+
'name', 'email', 'password', 'avatar'
2626
];
2727

2828
/**
@@ -31,6 +31,6 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
3131
* @var array
3232
*/
3333
protected $hidden = [
34-
'password', 'remember_token'
34+
'password', 'remember_token','oauth_provider_id', 'oauth_provider'
3535
];
3636
}

0 commit comments

Comments
 (0)