Skip to content

Commit a7ba7ae

Browse files
committed
Merge pull request silverbux#24 from silverbux/user-current-context
User current context
2 parents 9b76b7b + e899654 commit a7ba7ae

File tree

13 files changed

+98
-17
lines changed

13 files changed

+98
-17
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class LoginFormController {
2-
constructor ($auth, $state, $stateParams, API, AclService) {
2+
constructor ($rootScope, $auth, $state, $stateParams, API, AclService) {
33
'ngInject'
44

5+
delete $rootScope.me
6+
57
this.$auth = $auth
68
this.$state = $state
79
this.$stateParams = $stateParams

angular/app/components/nav-header/nav-header.component.html

+5-6
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,16 @@ <h3>
212212
<!-- User Account: style can be found in dropdown.less -->
213213
<li class="dropdown user user-menu" uib-dropdown>
214214
<a href="" class="dropdown-toggle" data-toggle="dropdown" uib-dropdown-toggle>
215-
<img src="/img/user2-160x160.jpg" class="user-image" alt="User Image">
216-
<span class="hidden-xs">Alexander Pierce</span>
215+
<img src="{{vm.userData.avatar}}" class="user-image" alt="User Image" onError="this.src='http://placeholdit.imgix.net/~text?txtfont=monospace,bold&bg=DD4B39&txtclr=ffffff&txt=A&w=45&h=45&txtsize=16'">
216+
<span class="hidden-xs">{{vm.userData.name | capitalize}}</span>
217217
</a>
218218
<ul class="dropdown-menu" uib-dropdown-menu>
219219
<!-- User image -->
220220
<li class="user-header">
221-
<img src="/img/user2-160x160.jpg" class="img-circle" alt="User Image">
222-
221+
<img src="{{vm.userData.avatar}}" class="img-circle" alt="User Image" onError="this.src='//placeholdit.imgix.net/~text?txtfont=monospace,bold&bg=DD4B39&txtclr=ffffff&txt=A&w=90&h=90&txtsize=36'">
223222
<p>
224-
Alexander Pierce - Web Developer
225-
<small>Member since Nov. 2012</small>
223+
{{vm.userData.name | capitalize}}
224+
<small>Member since {{vm.userData.created_at | datemillis |date:'MMMM yyyy' }}</small>
226225
</p>
227226
</li>
228227
<!-- Menu Body -->

angular/app/components/nav-header/nav-header.component.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
class NavHeaderController {
2-
constructor () {
2+
constructor ($rootScope, ContextService) {
33
'ngInject'
44

5-
//
5+
let navHeader = this
6+
7+
ContextService.me(function (data) {
8+
navHeader.userData = data
9+
})
610
}
11+
12+
$onInit () {}
713
}
814

915
export const NavHeaderComponent = {

angular/app/components/nav-sidebar/nav-sidebar.component.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<!-- Sidebar user panel -->
55
<div class="user-panel">
66
<div class="pull-left image">
7-
<img src="/img/user2-160x160.jpg" class="img-circle" alt="User Image">
7+
<img src="{{vm.userData.avatar}}" class="img-circle" alt="User Image" onError="this.src='http://placeholdit.imgix.net/~text?txtfont=monospace,bold&bg=DD4B39&txtclr=ffffff&txt=A&w=45&h=45&txtsize=16'">
88
</div>
99
<div class="pull-left info">
10-
<p>Alexander Pierce</p>
10+
<p>{{vm.userData.name | capitalize}}</p>
1111
<a href="javascript:void(0)"><i class="fa fa-circle text-success"></i> Online</a>
1212
</div>
1313
</div>

angular/app/components/nav-sidebar/nav-sidebar.component.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
class NavSidebarController {
2-
constructor (AclService) {
2+
constructor (AclService, ContextService) {
33
'ngInject'
44

5+
let navSideBar = this
56
this.can = AclService.can
7+
8+
ContextService.me(function (data) {
9+
navSideBar.userData = data
10+
})
611
}
712

813
$onInit () {}

angular/config/routes.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ export function RoutesConfig ($stateProvider, $urlRouterProvider) {
270270
url: '/logout',
271271
views: {
272272
'main@app': {
273-
controller: function ($scope, $auth, $state, AclService) {
273+
controller: function ($rootScope, $scope, $auth, $state, AclService) {
274274
$auth.logout().then(function () {
275+
delete $rootScope.me
275276
AclService.flushRoles()
276277
AclService.setAbilities({})
277278
$state.go('login')

angular/filters/date_millis.filter.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function DateMillisFilter () {
2+
'ngInject'
3+
4+
return function (input) {
5+
return Date.parse(input)
6+
}
7+
}

angular/index.filters.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DateMillisFilter } from './filters/date_millis.filter'
12
import { CapitalizeFilter } from './filters/capitalize.filter'
23
import { HumanReadableFilter } from './filters/human_readable.filter'
34
import { TruncatCharactersFilter } from './filters/truncate_characters.filter'
@@ -6,8 +7,9 @@ import { TrustHtmlFilter } from './filters/trust_html.filter'
67
import { UcFirstFilter } from './filters/ucfirst.filter'
78

89
angular.module('app.filters')
10+
.filter('datemillis', DateMillisFilter)
911
.filter('capitalize', CapitalizeFilter)
10-
.filter('humanReadable', HumanReadableFilter)
12+
.filter('humanreadable', HumanReadableFilter)
1113
.filter('truncateCharacters', TruncatCharactersFilter)
1214
.filter('truncateWords', TruncateWordsFilter)
1315
.filter('trustHtml', TrustHtmlFilter)

angular/index.services.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { ContextService } from './services/context.service'
12
import { APIService } from './services/API.service'
23
import { DialogService } from './services/dialog.service'
34
import { ToastService } from './services/toast.service'
45

56
angular.module('app.services')
7+
.service('ContextService', ContextService)
68
.service('API', APIService)
79
.service('DialogService', DialogService)
810
.service('ToastService', ToastService)

angular/run/routes.run.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function RoutesRun ($rootScope, $state, $auth, AclService, $timeout) {
1+
export function RoutesRun ($rootScope, $state, $auth, AclService, $timeout, API, ContextService) {
22
'ngInject'
33

44
AclService.resume()
@@ -15,8 +15,9 @@ export function RoutesRun ($rootScope, $state, $auth, AclService, $timeout) {
1515
$rootScope.bodyClass = 'hold-transition login-page'
1616
})
1717

18-
function fixSideBar () {
18+
function stateChange () {
1919
$timeout(function () {
20+
// fix sidebar
2021
var neg = $('.main-header').outerHeight() + $('.main-footer').outerHeight()
2122
var window_height = $(window).height()
2223
var sidebar_height = $('.sidebar').height()
@@ -30,10 +31,19 @@ export function RoutesRun ($rootScope, $state, $auth, AclService, $timeout) {
3031
$('.content-wrapper, .right-side').css('min-height', sidebar_height)
3132
}
3233
}
34+
35+
// get user current context
36+
if ($auth.isAuthenticated() && !$rootScope.me) {
37+
ContextService.getContext()
38+
.then((response) => {
39+
response = response.plain()
40+
$rootScope.me = response.data
41+
})
42+
}
3343
})
3444
}
3545

3646
$rootScope.$on('$destroy', deregisterationCallback)
37-
$rootScope.$on('$stateChangeSuccess', fixSideBar)
47+
$rootScope.$on('$stateChangeSuccess', stateChange)
3848
/*eslint-enable */
3949
}

angular/services/context.service.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export class ContextService {
2+
constructor ($auth, $rootScope, API) {
3+
'ngInject'
4+
this.$auth = $auth
5+
this.API = API
6+
this.$rootScope = $rootScope
7+
}
8+
9+
getContext () {
10+
let $auth = this.$auth
11+
12+
if ($auth.isAuthenticated()) {
13+
let API = this.API
14+
let UserData = API.service('me', API.all('users'))
15+
16+
return UserData.one().get()
17+
} else {
18+
return null
19+
}
20+
}
21+
22+
me (cb) {
23+
this.$rootScope.$watch('me', function (nv) {
24+
cb(nv)
25+
})
26+
}
27+
}

app/Http/Controllers/UserController.php

+10
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@
1414

1515
class UserController extends Controller
1616
{
17+
/**
18+
* Get user current context.
19+
*
20+
* @return JSON
21+
*/
1722
public function getMe()
1823
{
1924
$user = Auth::user();
2025

2126
return response()->success($user);
2227
}
2328

29+
/**
30+
* Update user current context.
31+
*
32+
* @return JSON success message
33+
*/
2434
public function putMe(Request $request)
2535
{
2636
$user = Auth::user();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ngDescribe({
2+
name: 'Test ContextService',
3+
modules: 'app',
4+
inject: 'ContextService',
5+
tests: function (deps) {
6+
it('basic test', () => {
7+
//
8+
})
9+
}
10+
})

0 commit comments

Comments
 (0)