Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor authentication and remove excessive directories #91

Open
wants to merge 21 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f6e72e1
Remove student library directive
shehzan2 Feb 7, 2015
52fe6c8
Remove student module directive
shehzan2 Feb 7, 2015
ca139a3
Student module link was off
shehzan2 Feb 7, 2015
4bc477b
Remove admin library directive. Statetracker syncs sidebar for module…
shehzan2 Feb 7, 2015
58195d5
Student module link was off
shehzan2 Feb 7, 2015
a6a76b5
Merge branch 'statetracker' into dev
shehzan2 Feb 7, 2015
8be6956
Remove admin module directive. Update side bar when adding lessons
shehzan2 Feb 7, 2015
228f409
Add resolvable authentication for better security
shehzan2 Feb 7, 2015
cf10239
Remove old asynchronous authservice. Create new top nav
shehzan2 Feb 7, 2015
2ce20ce
Removing old student top bar. Remove old authservice usage from it
shehzan2 Feb 7, 2015
6e1c899
Remove necessity of BarService and complete admin topbar
shehzan2 Feb 8, 2015
f995eae
Top bar complete without view as feature
shehzan2 Feb 8, 2015
be18b26
Sidebar built out partially
shehzan2 Feb 8, 2015
cd10673
Preload library and navbar info before loading page
shehzan2 Feb 8, 2015
3d523db
Taking care of edge case where we do not have library id
shehzan2 Feb 8, 2015
7da2802
View as feature implemented
shehzan2 Feb 8, 2015
1c1659e
Refactor AuthService and StateTracker into AuthService, Permissions, …
shehzan2 Feb 9, 2015
0cfa8cc
Remove user_view Ctrl
shehzan2 Feb 10, 2015
d97faf1
Update AuthService usage in admin topbar and sidebar
shehzan2 Feb 10, 2015
eefe18f
Refactoring complete for using AuthService and Permissions properly. …
sdevani Feb 10, 2015
d08fc4c
Fixing remaining bugs
sdevani Feb 12, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 39 additions & 61 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,82 @@
var app = angular.module("Lucere", ["ngResource", "ngRoute", "dndLists"])
.config(["$routeProvider", function($routeProvider) {

var addLibraryResolves = function(resolve) {
resolve.libraryState = function(StateTracker) {
return StateTracker.setLibraryState();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;

};

$routeProvider.whenAuth = function(url, options) {
options.resolve = options.resolve || {};
options.resolve.authorize = function(AuthService) {
return AuthService.authorizeStudent();
}

addLibraryResolves(options.resolve);
return this.when(url, options);
};

$routeProvider.whenAdmin = function(url, options) {
options.resolve = options.resolve || {};
options.resolve.authorize = function(AuthService) {
return AuthService.authorizeAdmin();
}

addLibraryResolves(options.resolve);
return this.when(url, options);
};

$routeProvider
.when("/login", {
templateUrl: "/js/templates/views/login.html"
})
.when("/admin/user/create", {
.whenAdmin("/admin/user/create", {
controller: "UserCreationCtrl",
templateUrl: "/js/templates/views/user_create.html"
})
.when("/admin/user/:userId", {
.whenAdmin("/admin/user/:userId", {
controller: "UserCtrl",
templateUrl: "/js/templates/views/user_profile.html"
})
.when("/admin/team/:teamId", {
.whenAdmin("/admin/team/:teamId", {
controller: "TeamCtrl",
templateUrl: "/js/templates/views/team.html"
})
.when("/admin/library/:libraryId", {
.whenAdmin("/admin/library/:libraryId", {
controller: "LibraryCtrl",
templateUrl: "/js/templates/views/admin_library.html"
})
.when("/admin/library/:libraryId/module/:moduleId", {
.whenAdmin("/admin/library/:libraryId/module/:moduleId", {
controller: "ModuleCtrl",
templateUrl: "/js/templates/views/admin_module.html"
})
.when("/admin/library/:libraryId/module/:moduleId/lesson/:lessonId", {
.whenAdmin("/admin/library/:libraryId/module/:moduleId/lesson/:lessonId", {
controller: "LessonCtrl",
templateUrl: "/js/templates/views/admin_lesson.html"
})
.when("/user/:userId", {
.whenAuth("/user/:userId", {
controller: "UserCtrl",
templateUrl: "/js/templates/views/user_profile.html"
})
.when("/team/:teamId", {
.whenAuth("/team/:teamId", {
controller: "TeamCtrl",
templateUrl: "/js/templates/views/team.html"
})
.when("/library/:libraryId", {
.whenAuth("/library/:libraryId", {
controller: "LibraryCtrl",
templateUrl: "/js/templates/views/student_library.html"
})
.when("/library/:libraryId/module/:moduleId", {
.whenAuth("/library/:libraryId/module/:moduleId", {
controller: "ModuleCtrl",
templateUrl: "/js/templates/views/student_module.html"
})
.when("/library/:libraryId/module/:moduleId/lesson/:lessonId", {
.whenAuth("/library/:libraryId/module/:moduleId/lesson/:lessonId", {
controller: "LessonCtrl",
templateUrl: "/js/templates/views/student_lesson.html"
})
.when("/error", {
templateUrl: "/js/templates/views/no_user_error.html"
})
.otherwise({redirectTo: "/login"});
}])
.run(["$rootScope", "$location", "AuthService", function($rootScope, $location, AuthService) {
$rootScope.$on("$routeChangeStart", function(e) {
if(!AuthService.loggedIn()) {
AuthService.login(function(user) {
if(user.teams.length === 0 && user.administrating.length === 0) {
$location.path("/user/"+user.id);
}
});
}
});
}])
.run(["$rootScope", "$location", "AuthService", "$route", "$routeParams", function($rootScope, $location, AuthService, $route, $routeParams) {
$rootScope.$on("$routeChangeSuccess", function(event, next, current) {
var routeParams = $route.current.params;
var libId = routeParams.libraryId;

var isAdminRoute = /(admin\/)/.test($location.path());

// If on a library route, check to see if user should have access
if(libId) {
AuthService.currentUser(function(user) {
if(user && user.teams) {
var allowAccess = false;

// Users accessing admin route must be admins
if(isAdminRoute) {
user.teams.forEach(function(team) {
user.administrating.forEach(function(admining) {
if(team.library && team.library == libId && admining.id == team.id) {
allowAccess = true;
}
})
});
} else {
user.teams.forEach(function(team) {
if(team.library && team.library == libId) {
allowAccess = true;
}
});
}
if(!allowAccess) {
$location.path("/user/" + user.id);
}
}
});
}

});
}]);
}]);
15 changes: 13 additions & 2 deletions assets/js/controllers/library_ctrl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
app.controller("LibraryCtrl", ["$scope", function($scope) {
app.controller("LibraryCtrl", ["$scope", "$routeParams", "Module", "StateTracker", function($scope, $routeParams, Module, StateTracker) {
$scope.library = StateTracker.loadLibrary($routeParams.libraryId);

}]);
$scope.createModule = function() {
var module = new Module({name: $scope.moduleName.name});
module.$save(function(data) {
$scope.library.modules.push(data.id);
$scope.library.$save(function(data) {
$scope.moduleName = "";
StateTracker.refreshLibrary();
});
});
};
}]);
24 changes: 22 additions & 2 deletions assets/js/controllers/module_ctrl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
app.controller("ModuleCtrl", ["$scope", function($scope) {
app.controller("ModuleCtrl", ["$scope", "Module", "$routeParams", "StateTracker", "Lesson", function($scope, Module, $routeParams, StateTracker, Lesson) {
var moduleId = $routeParams.moduleId;
$scope.module = Module.get({id: moduleId});

}]);
$scope.createLesson = function(title) {
var lesson = new Lesson({title: title});

lesson.$save(function(data) {
$scope.module.lessons.push(data.id);
$scope.module.$save()
.then(function() {
StateTracker.refreshLibrary();
});
$scope.newLessonTitle = "";
});
};

$scope.updateName = function() {
$scope.module.$update(function() {
StateTracker.refreshLibrary();
});
};
}]);
3 changes: 3 additions & 0 deletions assets/js/controllers/nav_ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
app.controller("NavCtrl", ['$scope', 'Permissions', function($scope, Permissions) {
$scope.Permissions = Permissions;
}]);
86 changes: 12 additions & 74 deletions assets/js/controllers/team_ctrl.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,33 @@
app.controller("TeamCtrl", ["$scope", "$routeParams", "Team", "User", "AuthService", function($scope, $routeParams, Team, User, AuthService){
app.controller("TeamCtrl", ["$scope", "$routeParams", "Team", "AuthService", "Permissions", function($scope, $routeParams, Team, AuthService, Permissions){
var teamId = parseInt($routeParams.teamId);
$scope.team = Team.get({id: teamId});
$scope.newUser = {};
$scope.newTeam = {};
$scope.isAdmin = false;
$scope.isCore = teamId === 1;
var currentUser;

var getAdminStatus = function() {
AuthService.currentUser(function(user) {
currentUser = user;
$scope.isAdmin = user.administrating.reduce(function(a, b) {
return a || b.id === teamId;
}, false);
});
}

getAdminStatus();

$scope.adminsThis = function(thisUserId) {
var admining = false;
$scope.team.admins.forEach(function(admin) {
if(admin.id == thisUserId) {
admining = true;
}
});
return admining;
}
$scope.newAdmin = {};
$scope.AuthService = AuthService;

// Gets user from server
// executes successCb or failureCb depending on server response
var getUser = function(userName, successCb, failureCb) {
var userPromise = User.UserFindBy.get({github: userName});
userPromise.$promise.then(
function(user) {
if(successCb) {
successCb(user);
}
}, function(response) {
if(failureCb) {
failureCb(response);
}
}
);
}

$scope.addUser = function() {
var _this = this;
var userName = $scope.newUser.name;
var userFound = function(user) {
$scope.team.users.push(user);
$scope.team.$update();
};
var userNotFound = function(response) {
alert(userName + " is not a user yet.\nGo to /admin/user/create || /user/create.")
};
getUser(userName, userFound, userNotFound);
$scope.newUser.name = "";
TeamManager.addUserByName($scope.newUser.name, $scope.team);
$scope.newUser = {};
};

$scope.addAdmin = function() {
var userName = $scope.newAdmin.name;
var userFound = function(user) {
$scope.team.users.push(user); // by default, add admin to team users collection
$scope.team.admins.push(user);
$scope.team.$update();
};
var userNotFound = function(response) {
alert(userName + " is not a user yet.\nGo to /admin/user/create || /user/create.")
};
getUser(userName, userFound, userNotFound);
$scope.newAdmin.name = "";
TeamManager.addAdminByName($scope.newAdmin.name, $scope.team);
$scope.newAdmin = {};
}

$scope.remove = function(userId) {
$scope.team.users = $scope.team.users.filter(function(v) {
if(v.id !== userId) {
return v;
}
});
$scope.team.$update();
TeamManager.removeUserById(userId, $scope.team);
};

$scope.addTeam = function() {
var newTeamName = $scope.newTeam.name;
$scope.newTeam.name = "";
var newTeam = new Team({
name: newTeamName,
admins: [currentUser],
users: [currentUser]
name: $scope.newTeam.name,
admins: [AuthService.currentUser],
users: [AuthService.currentUser]
});
newTeam.$save();
$scope.newTeam = {};
}
}]);
9 changes: 3 additions & 6 deletions assets/js/controllers/user_ctrl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
app.controller("UserCtrl", ["$scope", "$routeParams", "User", "AuthService", function($scope, $routeParams, User, AuthService) {
var userId = parseInt($routeParams.userId);
var userId = $routeParams.userId;
var userRecord = User.User.get({id: userId});
$scope.userForm = {name: "", email: "", github: "", twitter: ""};
$scope.userPar = {name: "", email: "", github: "", twitter: ""};
Expand All @@ -8,12 +8,9 @@ app.controller("UserCtrl", ["$scope", "$routeParams", "User", "AuthService", fun
setUserPar();
});

$scope.isOwn = false;
$scope.showForm = false;
var currentUser;
AuthService.currentUser(function(user) {
$scope.isOwn = (userId === user.id);
});

$scope.isOwn = (userId == AuthService.currentUser.id);

var setValues = function(toBeSet, getValsFrom) {
var keys = Object.keys(getValsFrom);
Expand Down
5 changes: 0 additions & 5 deletions assets/js/controllers/user_view_ctrl.js

This file was deleted.

20 changes: 0 additions & 20 deletions assets/js/directives/admin_library_directive.js

This file was deleted.

39 changes: 0 additions & 39 deletions assets/js/directives/admin_module_directive.js

This file was deleted.

Loading