Skip to content

Commit

Permalink
Revert "refact(authentication"
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Mar 8, 2013
1 parent 045fcfd commit 21c38aa
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 80 deletions.
4 changes: 2 additions & 2 deletions client/src/app/admin/projects/admin-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ angular.module('admin-projects', ['resources.projects', 'resources.users', 'serv

.config(['crudRouteProvider', function (crudRouteProvider) {

var adminUser = ['security', function(security) {
return security.requireAdminUser();
var adminUser = ['authentication', function(authentication) {
return authentication.requireAdminUser();
}];

var getAllUsers = ['Projects', 'Users', '$route', function(Projects, Users, $route){
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/admin/users/admin-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ angular.module('admin-users', ['admin-users-edit', 'services.crud', 'services.i1

.config(['crudRouteProvider', function (crudRouteProvider) {

var adminUser = ['security', function(security) {
return security.requireAdminUser();
var adminUser = ['authentication', function(authentication) {
return authentication.requireAdminUser();
}];

crudRouteProvider.routesFor('Users', 'admin')
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ angular.module('app', [
'services.breadcrumbs',
'services.i18nNotifications',
'services.httpRequestTracker',
'security',
'authentication',
'directives.crud',
'templates.app',
'templates.common']);
Expand Down
8 changes: 4 additions & 4 deletions client/src/app/header.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<div class="navbar-inner">
<a class="brand" ng-click="home()">AScrum</a>
<ul class="nav">
<li ng-class="{active:isNavbarActive('projectsinfo')}"><a href="/projectsinfo">Current Projects</a></li>
<li ng-class="{active:isNavbarActive('projectsinfo')}"><a href="/projectsinfo">Projects info</a></li>
</ul>

<ul class="nav" ng-show="currentUser.isAuthenticated()">
<li ng-class="{active:isNavbarActive('projects')}"><a href="/projects">My Projects</a></li>
<li ng-class="{active:isNavbarActive('projects')}"><a href="/projects">My projects</a></li>
<li class="dropdown" ng-class="{active:isNavbarActive('admin'), open:isAdminOpen}" ng-show="currentUser.isAdmin()">
<a id="adminmenu" role="button" class="dropdown-toggle" ng-click="isAdminOpen=!isAdminOpen">Admin<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="adminmenu">
<li><a tabindex="-1" href="/admin/projects" ng-click="isAdminOpen=false">Manage Projects</a></li>
<li><a tabindex="-1" href="/admin/users" ng-click="isAdminOpen=false">Manage Users</a></li>
<li><a tabindex="-1" href="/admin/projects" ng-click="isAdminOpen=false">Projects</a></li>
<li><a tabindex="-1" href="/admin/users" ng-click="isAdminOpen=false">Users</a></li>
</ul>
</li>
</ul>
Expand Down
2 changes: 2 additions & 0 deletions client/src/common/authentication/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Based loosely around work by Witold Szczerba - https://github.com/witoldsz/angular-http-auth
angular.module('authentication', ['authentication.service', 'authentication.interceptor', 'authentication.login']);
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Based loosely around work by Witold Szczerba - https://github.com/witoldsz/angular-http-auth
angular.module('security.service', [
'security.currentUser', 'security.retryQueue', 'security.login', 'ui.bootstrap.dialog'
angular.module('authentication.service', [
'authentication.currentUser', 'authentication.retryQueue', 'authentication.login', 'ui.bootstrap.dialog'
])

// The security is the public API for this module. Application developers should only need to use this service and not any of the others here.
.factory('security',
['$rootScope', '$http', '$location', '$q', 'securityRetryQueue', 'currentUser', '$dialog',
// The authentication is the public API for this module. Application developers should only need to use this service and not any of the others here.
.factory('authentication',
['$rootScope', '$http', '$location', '$q', 'authenticationRetryQueue', 'currentUser', '$dialog',
function( $rootScope, $http, $location, $q, queue, currentUser, $dialog) {

// We need a way to refresh the page to clear any data that has been loaded when the user logs out
Expand All @@ -19,7 +19,7 @@ angular.module('security.service', [
function openLoginDialog() {
if ( !loginDialog ) {
loginDialog = $dialog.dialog();
loginDialog.open('security/login/form.tpl.html', 'LoginFormController').then(onLoginDialogClose);
loginDialog.open('authentication/login/form.tpl.html', 'LoginFormController').then(onLoginDialogClose);
}
}
function closeLoginDialog(success) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module('security.currentUser', [])
angular.module('authentication.currentUser', [])

// The current user. You can watch this for changes due to logging in and out
.factory('currentUser', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
angular.module('security.interceptor', ['security.retryQueue'])
angular.module('authentication.interceptor', ['authentication.retryQueue'])

// This http interceptor listens for security failures
.factory('securityInterceptor', ['$injector', 'securityRetryQueue', function($injector, queue) {
// This http interceptor listens for authentication failures
.factory('authenticationInterceptor', ['$injector', 'authenticationRetryQueue', function($injector, queue) {
return function(promise) {
// Intercept failed requests
return promise.then(null, function(originalResponse) {
Expand All @@ -19,5 +19,5 @@ angular.module('security.interceptor', ['security.retryQueue'])

// We have to add the interceptor to the queue as a string because the interceptor depends upon service instances that are not available in the config block.
.config(['$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push('securityInterceptor');
$httpProvider.responseInterceptors.push('authenticationInterceptor');
}]);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
angular.module('security.login.form', ['services.localizedMessages'])
angular.module('authentication.login.form', ['services.localizedMessages'])

// The LoginFormController provides the behaviour behind a reusable form to allow users to authenticate.
// This controller and its template (login/form.tpl.html) are used in a modal dialog box by the security service.
.controller('LoginFormController', ['$scope', 'security', 'localizedMessages', 'currentUser', function($scope, security, localizedMessages, currentUser) {
// This controller and its template (login/form.tpl.html) are used in a modal dialog box by the authentication service.
.controller('LoginFormController', ['$scope', 'authentication', 'localizedMessages', 'currentUser', function($scope, authentication, localizedMessages, currentUser) {
// The model for this form
$scope.user = {};

Expand All @@ -12,19 +12,19 @@ angular.module('security.login.form', ['services.localizedMessages'])
// The reason that we are being asked to login - for instance because we tried to access something to which we are not authorized
// We could do something diffent for each reason here but to keep it simple...
$scope.authReason = null;
if ( security.getLoginReason() ) {
if ( authentication.getLoginReason() ) {
$scope.authReason = ( currentUser.isAuthenticated() ) ?
localizedMessages.get('login.reason.notAuthorized') :
localizedMessages.get('login.reason.notAuthenticated');
}

// Attempt to authenticate the user specified in the form's model
$scope.login = function() {
// Clear any previous security errors
// Clear any previous authentication errors
$scope.authError = null;

// Try to login
security.login($scope.user.email, $scope.user.password).then(function(loggedIn) {
authentication.login($scope.user.email, $scope.user.password).then(function(loggedIn) {
if ( !loggedIn ) {
// If we get here then the login failed due to bad credentials
$scope.authError = localizedMessages.get('login.error.invalidCredentials');
Expand All @@ -40,6 +40,6 @@ angular.module('security.login.form', ['services.localizedMessages'])
};

$scope.cancelLogin = function() {
security.cancelLogin();
authentication.cancelLogin();
};
}]);
1 change: 1 addition & 0 deletions client/src/common/authentication/login/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
angular.module('authentication.login', ['authentication.login.form', 'authentication.login.toolbar']);
19 changes: 19 additions & 0 deletions client/src/common/authentication/login/toolbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
angular.module('authentication.login.toolbar', [])

// The loginToolbar directive is a reusable widget that can show login or logout buttons
// and information the current authenticated user
.directive('loginToolbar', ['currentUser', 'authentication', function(currentUser, authentication) {
var directive = {
templateUrl: 'authentication/login/toolbar.tpl.html',
restrict: 'E',
replace: true,
scope: true,
link: function($scope, $element, $attrs, $controller) {
$scope.userInfo = currentUser.info;
$scope.isAuthenticated = currentUser.isAuthenticated;
$scope.logout = function() { authentication.logout(); };
$scope.login = function() { authentication.showLogin(); };
}
};
return directive;
}]);
16 changes: 16 additions & 0 deletions client/src/common/authentication/login/toolbar.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<ul class="nav pull-right">
<li class="divider-vertical"></li>
<li ng-show="isAuthenticated()">
<a href="#">{{userInfo().firstName}} {{userInfo().lastName}}</a>
</li>
<li ng-show="isAuthenticated()" class="logout">
<form class="navbar-form">
<button class="btn" ng-click="logout()">Log out</button>
</form>
</li>
<li ng-hide="isAuthenticated()" class="login">
<form class="navbar-form">
<button class="btn" ng-click="login()">Log in</button>
</form>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
angular.module('security.retryQueue', [])
angular.module('authentication.retryQueue', [])

// This is a generic retry queue for security failures. Each item is expected to expose two functions: retry and cancel.
.factory('securityRetryQueue', ['$q', function($q) {
// This is a generic retry queue for authentication failures. Each item is expected to expose two functions: retry and cancel.
.factory('authenticationRetryQueue', ['$q', function($q) {
var retryQueue = [];
var service = {
// The security service puts its own handler in here!
// The authentication service puts its own handler in here!
onItemAdded: angular.noop,

hasMore: function() {
Expand Down
2 changes: 0 additions & 2 deletions client/src/common/security/index.js

This file was deleted.

1 change: 0 additions & 1 deletion client/src/common/security/login/login.js

This file was deleted.

17 changes: 0 additions & 17 deletions client/src/common/security/login/toolbar.js

This file was deleted.

16 changes: 0 additions & 16 deletions client/src/common/security/login/toolbar.tpl.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
describe('security', function() {
describe('authentication', function() {

var $rootScope, $http, $httpBackend, success, error, status, userResponse;

angular.module('test',[]).constant('I18N.MESSAGES', messages = {});
beforeEach(module('security', 'test', 'security/login/form.tpl.html'));
beforeEach(module('authentication', 'test', 'authentication/login/form.tpl.html'));
beforeEach(inject(function(_$rootScope_, _$httpBackend_, _$http_) {
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
Expand All @@ -24,9 +24,9 @@ describe('security', function() {

var service, currentUser, queue;
beforeEach(inject(function($injector) {
service = $injector.get('security');
service = $injector.get('authentication');
currentUser = $injector.get('currentUser');
queue = $injector.get('securityRetryQueue');
queue = $injector.get('authenticationRetryQueue');
}));

describe('showLogin', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe("currentUser", function() {
beforeEach(module('security.currentUser'));
beforeEach(module('authentication.currentUser'));

it("should be unauthenticated to begin with", function() {
inject(function(currentUser) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
describe('securityInterceptor', function() {
describe('authenticationInterceptor', function() {
var queue, interceptor, promise, wrappedPromise;

beforeEach(module('security.interceptor'));
beforeEach(module('authentication.interceptor'));

beforeEach(inject(function($injector) {
queue = $injector.get('securityRetryQueue');
interceptor = $injector.get('securityInterceptor');
queue = $injector.get('authenticationRetryQueue');
interceptor = $injector.get('authenticationInterceptor');
wrappedPromise = {};
promise = {
then: jasmine.createSpy('then').andReturn(wrappedPromise)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe('login-toolbar', function() {
var userInfo = { id: '1234567890', email: '[email protected]', firstName: 'Jo', lastName: 'Bloggs'};
var scope, toolbar;

beforeEach(module('security/login/toolbar.tpl.html', 'security'));
beforeEach(module('authentication/login/toolbar.tpl.html', 'authentication'));

beforeEach(inject(function($httpBackend, $rootScope, $compile) {
$httpBackend.when('GET', '/current-user').respond(200, { user: userInfo });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
describe('securityRetryQueue', function() {
describe('authenticationRetryQueue', function() {
var queue;

function mockRetryItem() {
return jasmine.createSpyObj('retryItem', ['retry', 'cancel']);
}

beforeEach(module('security.retryQueue'));
beforeEach(module('authentication.retryQueue'));

beforeEach(inject(function($injector) {
queue = $injector.get('securityRetryQueue');
queue = $injector.get('authenticationRetryQueue');
}));

describe('hasMore', function() {
Expand Down

0 comments on commit 21c38aa

Please sign in to comment.