-
Notifications
You must be signed in to change notification settings - Fork 3
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
sdevani
wants to merge
21
commits into
makersquare:dev
Choose a base branch
from
sdevani:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 52fe6c8
Remove student module directive
shehzan2 ca139a3
Student module link was off
shehzan2 4bc477b
Remove admin library directive. Statetracker syncs sidebar for module…
shehzan2 58195d5
Student module link was off
shehzan2 a6a76b5
Merge branch 'statetracker' into dev
shehzan2 8be6956
Remove admin module directive. Update side bar when adding lessons
shehzan2 228f409
Add resolvable authentication for better security
shehzan2 cf10239
Remove old asynchronous authservice. Create new top nav
shehzan2 2ce20ce
Removing old student top bar. Remove old authservice usage from it
shehzan2 6e1c899
Remove necessity of BarService and complete admin topbar
shehzan2 f995eae
Top bar complete without view as feature
shehzan2 be18b26
Sidebar built out partially
shehzan2 cd10673
Preload library and navbar info before loading page
shehzan2 3d523db
Taking care of edge case where we do not have library id
shehzan2 7da2802
View as feature implemented
shehzan2 1c1659e
Refactor AuthService and StateTracker into AuthService, Permissions, …
shehzan2 0cfa8cc
Remove user_view Ctrl
shehzan2 d97faf1
Update AuthService usage in admin topbar and sidebar
shehzan2 eefe18f
Refactoring complete for using AuthService and Permissions properly. …
sdevani d08fc4c
Fixing remaining bugs
sdevani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
|
||
$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); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
}); | ||
}]); | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}; | ||
} | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
;