diff --git a/assets/js/app.js b/assets/js/app.js index ae187ff..0514bde 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -1,50 +1,77 @@ 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" }) @@ -52,53 +79,4 @@ var app = angular.module("Lucere", ["ngResource", "ngRoute", "dndLists"]) 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); - } - } - }); - } - - }); - }]); + }]); \ No newline at end of file diff --git a/assets/js/controllers/library_ctrl.js b/assets/js/controllers/library_ctrl.js index 871056d..e2a254a 100644 --- a/assets/js/controllers/library_ctrl.js +++ b/assets/js/controllers/library_ctrl.js @@ -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); -}]); \ No newline at end of file + $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(); + }); + }); + }; +}]); diff --git a/assets/js/controllers/module_ctrl.js b/assets/js/controllers/module_ctrl.js index 1405e61..61c8952 100644 --- a/assets/js/controllers/module_ctrl.js +++ b/assets/js/controllers/module_ctrl.js @@ -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}); -}]); \ No newline at end of file + $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(); + }); + }; +}]); diff --git a/assets/js/controllers/nav_ctrl.js b/assets/js/controllers/nav_ctrl.js new file mode 100644 index 0000000..8e24c3a --- /dev/null +++ b/assets/js/controllers/nav_ctrl.js @@ -0,0 +1,3 @@ +app.controller("NavCtrl", ['$scope', 'Permissions', function($scope, Permissions) { + $scope.Permissions = Permissions; +}]); diff --git a/assets/js/controllers/team_ctrl.js b/assets/js/controllers/team_ctrl.js index 740348c..4b1ea69 100644 --- a/assets/js/controllers/team_ctrl.js +++ b/assets/js/controllers/team_ctrl.js @@ -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 = {}; } }]); diff --git a/assets/js/controllers/user_ctrl.js b/assets/js/controllers/user_ctrl.js index e44259c..b82baee 100644 --- a/assets/js/controllers/user_ctrl.js +++ b/assets/js/controllers/user_ctrl.js @@ -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: ""}; @@ -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); diff --git a/assets/js/controllers/user_view_ctrl.js b/assets/js/controllers/user_view_ctrl.js deleted file mode 100644 index 6bbfa7d..0000000 --- a/assets/js/controllers/user_view_ctrl.js +++ /dev/null @@ -1,5 +0,0 @@ -app.controller("UserViewCtrl", ["$scope", "User", "AuthService", function($scope, User, AuthService){ - AuthService.currentUser(function(user) { - $scope.admin = (user.administrating.length !== 0); - }); -}]); \ No newline at end of file diff --git a/assets/js/directives/admin_library_directive.js b/assets/js/directives/admin_library_directive.js deleted file mode 100644 index f338b3d..0000000 --- a/assets/js/directives/admin_library_directive.js +++ /dev/null @@ -1,20 +0,0 @@ -app.directive("adminLibraryDirective", ["Library", "$route", "Module", function(Library, $route, Module) { - return { - templateUrl: "/js/templates/directives/admin_library_template.html", - link: function(scope, attr, elem) { - scope.params = $route.current.params; - var id = scope.params.libraryId; - scope.library = Library.get({id: id}); - - 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 = ""; - }); - }); - }; - } - }; -}]); \ No newline at end of file diff --git a/assets/js/directives/admin_module_directive.js b/assets/js/directives/admin_module_directive.js deleted file mode 100644 index 7610db1..0000000 --- a/assets/js/directives/admin_module_directive.js +++ /dev/null @@ -1,39 +0,0 @@ -app.directive("adminModuleDirective", ["$route", "Module", "Lesson", function($route, Module, Lesson) { - return { - templateUrl: "/js/templates/directives/admin_module_template.html", - link: function(scope, attr, elem) { - scope.params = $route.current.params; - scope.module = Module.get({id: scope.params.moduleId}, function(data) { - scope.currentName = data.name; - }); - - scope.createLesson = function(title) { - var lesson = new Lesson({title: title}); - // Save the new lesson to the database - // Push the new lesson into the module.lessons array - // Save the module with the newly added lesson - lesson.$save(function(data) { - scope.module.lessons.push(data.id); - scope.module.$save(function(data) { - scope.newLessonTitle = ""; - }); - }); - }; - - // Activate "Save Changes" button if the new name doesn't equal the original name - scope.checkName = function(moduleName) { - if (moduleName === scope.currentName) { - return true; - } else { - return false; - } - }; - - scope.updateName = function() { - scope.module.$update(function() { - $route.reload(); - }); - }; - } - }; -}]); \ No newline at end of file diff --git a/assets/js/directives/admin_sidebar_directive.js b/assets/js/directives/admin_sidebar_directive.js index c05ae07..ed44596 100644 --- a/assets/js/directives/admin_sidebar_directive.js +++ b/assets/js/directives/admin_sidebar_directive.js @@ -1,10 +1,8 @@ -app.directive("adminSidebarDirective", ["$route", "Module", function($route, Module) { +app.directive("adminSidebar", ["$route", "StateTracker", function($route, StateTracker) { return { - templateUrl: "/js/templates/directives/admin_sidebar_template.html", + templateUrl: "/js/templates/directives/admin_sidebar.html", link: function(scope, attr, elem) { - Module.query({library: $route.current.params.libraryId}, function(data) { - scope.modules = data; - }); + scope.StateTracker = StateTracker; } }; }]); \ No newline at end of file diff --git a/assets/js/directives/admin_top_nav_directive.js b/assets/js/directives/admin_top_nav_directive.js deleted file mode 100644 index 6fd244b..0000000 --- a/assets/js/directives/admin_top_nav_directive.js +++ /dev/null @@ -1,35 +0,0 @@ -app.directive("adminTopNavDirective", ["BarService", function(BarService) { - return { - templateUrl: "/js/templates/directives/admin_top_nav_template.html", - link: function(scope, attr, elem) { - BarService.setLinks(scope, attr, elem, function($route) { - scope.userArr = scope.currentUser.administrating; - var teams = scope.currentUser.administrating; - var id = $route.current.params.libraryId || $route.current.params.teamId; - if(scope.teamList.length > 1) { - scope.dropCheck = true; - } else { - scope.dropCheck = false; - } - for(var i = 0; i < teams.length; i++) { - if(id == 1) { - scope.libraryTag = "Core"; - scope.userArr.shift(); - } else if(teams[i].id == id) { - scope.libraryTag = teams[i].name; - //loop through user array to remove the name that will be displayed on nav bar - for(var j = 0; j < scope.userArr.length; j++) { - if(teams[i].name === scope.userArr[j].name) { - scope.userArr.splice(j,1); - } - } - //first item in list should be core or core should be displayed on nav bar - if(scope.userArr[0].name !== "Core") { - scope.userArr.unshift({id:1, name:"Core"}); - } - } - } - }); - } - }; -}]); \ No newline at end of file diff --git a/assets/js/directives/admin_topbar_directive.js b/assets/js/directives/admin_topbar_directive.js new file mode 100644 index 0000000..96341f8 --- /dev/null +++ b/assets/js/directives/admin_topbar_directive.js @@ -0,0 +1,10 @@ +app.directive("adminTopbar", ["AuthService", "StateTracker", "Permissions", function(AuthService, StateTracker, Permissions) { + return { + templateUrl: "/js/templates/directives/admin_topbar.html", + link: function(scope, attr, elem) { + scope.AuthService = AuthService; + scope.StateTracker = StateTracker; + scope.Permissions = Permissions; + } + }; +}]); diff --git a/assets/js/directives/student_library_directive.js b/assets/js/directives/student_library_directive.js deleted file mode 100644 index 6596dce..0000000 --- a/assets/js/directives/student_library_directive.js +++ /dev/null @@ -1,10 +0,0 @@ -app.directive("studentLibraryDirective", ["Library", "$route", function(Library, $route) { - return { - templateUrl: "/js/templates/directives/student_library_template.html", - link: function(scope, attr, elem) { - scope.params = $route.current.params; - var id = scope.params.libraryId; - scope.library = Library.get({id: id}); - } - }; -}]); \ No newline at end of file diff --git a/assets/js/directives/student_module_directive.js b/assets/js/directives/student_module_directive.js deleted file mode 100644 index 96fa0ad..0000000 --- a/assets/js/directives/student_module_directive.js +++ /dev/null @@ -1,11 +0,0 @@ -app.directive("studentModuleDirective", ["$route", "Module", "Lesson", function($route, Module, Lesson) { - return { - templateUrl: "/js/templates/directives/student_module_template.html", - link: function(scope, attr, elem) { - scope.params = $route.current.params; - scope.module = Module.get({id: scope.params.moduleId}, function(data) { - scope.currentName = data.name; - }); - } - }; -}]); \ No newline at end of file diff --git a/assets/js/directives/student_sidebar_directive.js b/assets/js/directives/student_sidebar_directive.js index 2db7b79..ad1fd8e 100644 --- a/assets/js/directives/student_sidebar_directive.js +++ b/assets/js/directives/student_sidebar_directive.js @@ -1,14 +1,8 @@ -app.directive("studentSidebarDirective", ["Module", "AuthService", function(Module, AuthService) { +app.directive("studentSidebar", ["StateTracker", function(StateTracker) { return { - templateUrl: "/js/templates/directives/student_sidebar_template.html", + templateUrl: "/js/templates/directives/student_sidebar.html", link: function(scope, attr, elem) { - AuthService.currentUser(function(user) { - if(user) { - Module.query({library: user.teams[0].library}, function(data) { - scope.modules = data; - }); - } - }); + scope.StateTracker = StateTracker; } }; }]); \ No newline at end of file diff --git a/assets/js/directives/student_top_bar_directive.js b/assets/js/directives/student_top_bar_directive.js deleted file mode 100644 index 3f0b663..0000000 --- a/assets/js/directives/student_top_bar_directive.js +++ /dev/null @@ -1,8 +0,0 @@ -app.directive("studentTopBarDirective", ["BarService", function(BarService) { - return { - templateUrl: "/js/templates/directives/student_top_bar_template.html", - link: function(scope, attr, elem) { - BarService.setLinks(scope, attr, elem); - } - }; -}]); \ No newline at end of file diff --git a/assets/js/directives/student_topbar_directive.js b/assets/js/directives/student_topbar_directive.js new file mode 100644 index 0000000..df3a5ea --- /dev/null +++ b/assets/js/directives/student_topbar_directive.js @@ -0,0 +1,10 @@ +app.directive("studentTopbar", ["AuthService", "StateTracker", "Permissions", function(AuthService, StateTracker, Permissions) { + return { + templateUrl: "/js/templates/directives/student_topbar.html", + link: function(scope, attr, elem) { + scope.AuthService = AuthService; + scope.StateTracker = StateTracker; + scope.Permissions = Permissions; + } + }; +}]); \ No newline at end of file diff --git a/assets/js/services/auth_service.js b/assets/js/services/auth_service.js index 3e48a44..7283c3e 100644 --- a/assets/js/services/auth_service.js +++ b/assets/js/services/auth_service.js @@ -1,43 +1,124 @@ -app.factory("AuthService", ["$http", "$location", function($http, $location) { - var user = null; +// app.factory("CachedLibraries", function() { - var logout = function() { - user = null; - $location.path("/login"); - $http.get("/logout"); +// }); + +app.factory("AuthService", ["$http", "$location", "StateTracker", function($http, $location, StateTracker) { + var service = {}; + var request; + + service.isLoggedIn = function() { + return !!service.currentUser; + }; + + service.isAdmin = function() { + if (!service.isLoggedIn()) { return false; } + return service.currentUser.administrating.length > 0; + }; + + service.isSuperAdmin = service.isAdmin; + + var hasAccess = function(options) { + if (!service.isLoggedIn()) { return false; } + + var listKey = options.admin ? "administrating" : "teams"; + var itemKey = options.library ? "library" : "id"; + + var result = false; + service.currentUser[listKey].forEach(function(item) { + if (item[itemKey] == options.id) { + result = true; + } + }) + return result; } - var login = function(cb) { - var request = $http({ - method: "GET", - url: "/user/currentuser" + service.isTeamMember = function(teamId) { + return hasAccess({ + id: teamId + }); + }; + + service.isTeamAdmin = function(teamId) { + return hasAccess({ + id: teamId, + admin: true + }); + }; + + service.checkTeamAdmin = function(team, userId) { + var res = false; + team.admins.forEach(function(admin) { + if (admin.id == userId) { + res = true; + } }); + return res; + }; + + service.isLibraryMember = function(libraryId) { + return hasAccess({ + id: libraryId, + library: true + }); + }; + + service.isLibraryMember = function(libraryId, teamId) { + var result = false; + service.currentUser.teams.forEach(function(team) { + if (team.library == libraryId && teamId == team.id) { + result = true; + } + }); + return true; + }; + + service.isLibraryAdmin = function(libraryId) { + return hasAccess({ + id: libraryId, + library: true, + admin: true + }); + }; - request.success(function(userData) { - user = userData; - if(cb) { - return cb(user); + service.authorizeStudent = function(cb) { + return userRequest().success(function(data) { + if (!data) { + $location.path("/login"); + } else if(cb) { + cb(data); } }) .error(function() { - return cb(null); + $location.path("/login"); }); }; - return { - login: login, - logout: logout, - - currentUser: function(cb) { - if(user) { - return cb(user); + service.authorizeAdmin = function() { + return service.authorizeStudent(function(data) { + if (!data.administrating || !data.administrating.length) { + $location.path("/user/" + data.id); } + }); + }; - login(cb); - }, + var userRequest = function() { + request = request || $http({ + method: "GET", + url: "/user/currentuser" + }); + request.success(function(data) { + service.currentUser = data; + }); + return request; + }; - loggedIn: function() { - return (user ? true : false); - } + service.logout = function() { + user = null; + request = null; + service.currentUser = null; + $location.path("/login"); + $http.get("/logout"); }; + + return service; }]); \ No newline at end of file diff --git a/assets/js/services/bar_service.js b/assets/js/services/bar_service.js deleted file mode 100644 index 17aa269..0000000 --- a/assets/js/services/bar_service.js +++ /dev/null @@ -1,44 +0,0 @@ -app.factory("BarService", ["$location", "$route", "AuthService", function($location, $route, AuthService) { - var setLinks = function(scope, attr, elem, cb) { - AuthService.currentUser(function(user) { - scope.currentUser = user; - scope.teamList = user.teams; - - if(user.teams) { - scope.libraries = []; - user.teams.forEach(function(t) { - scope.libraries.push(t.library); - }); - } - - if(cb) { - cb($route); - } - }); - - scope.logout = function() { - AuthService.logout(); - }; - - scope.getAdminLibrary = function(lId) { - $location.path("/admin/library/"+lId); - }; - - scope.getAdminTeam = function(tId) { - $location.path("/admin/team/"+tId); - }; - - scope.getLibrary = function(lId) { - $location.path("/library/"+lId); - }; - - scope.getTeam = function(tId) { - $location.path("/team/"+tId); - }; - - }; - - return { - setLinks: setLinks - }; -}]); \ No newline at end of file diff --git a/assets/js/services/permissions_service.js b/assets/js/services/permissions_service.js new file mode 100644 index 0000000..0bb0308 --- /dev/null +++ b/assets/js/services/permissions_service.js @@ -0,0 +1,59 @@ +app.factory("Permissions", ["AuthService", function(AuthService) { + var service = {}; + var viewAsEnabled = false; + var viewingAsTeam; + + service.viewAsButton = function() { + return AuthService.isAdmin(); + } + + service.teamView = function(teamId) { + return AuthService.isTeamMember(teamId); + }; + + service.teamEdit = function(teamId) { + var teamRights = AuthService.isTeamAdmin(teamId); + return teamRights && !viewAsEnabled; + }; + + service.teamCreate = function() { + return AuthService.isSuperAdmin(); + }; + + service.libraryView = function(libraryId) { + if (viewAsEnabled) { + return AuthService.isLibraryMember(libraryId); + } else { + return AuthService.isLibraryMember(libraryId, viewingAsTeam); + } + }; + + service.libraryEdit = function(libraryId) { + var libraryRights = AuthService.isLibraryAdmin(libraryId); + return libraryRights && !viewAsEnabled; + }; + + service.showAdminLinks = function() { + return AuthService.isAdmin() && !viewAsEnabled + }; + + service.viewAsTeam = function(teamId) { + if (AuthService.isAdmin() && AuthService.isTeamMember(teamId)) { + viewAsEnabled = true; + viewAsTeam = teamId; + return true; + } + return false; + }; + + service.viewAsAdmin = function() { + if (AuthService.isAdmin()) { + viewAsEnabled = false; + viewingAsTeam = undefined; + return true; + } + return false; + }; + + return service; +}]); diff --git a/assets/js/services/state_tracker.js b/assets/js/services/state_tracker.js new file mode 100644 index 0000000..39051c5 --- /dev/null +++ b/assets/js/services/state_tracker.js @@ -0,0 +1,74 @@ +app.factory("StateTracker", ["Library", "Module", "$route", "$q", '$location', function(Library, Module, $route, $q, $location) { + var currentState = {}; + + var viewAsMode = false; + + var currentUser; + + currentState.showAdminFunctionality = false; + + currentState.viewAs = function(team) { + viewAsMode = true; + currentState.viewAsTeam = team; + currentState.updateViewAsPreference(); + $location.path("/library/" + team.id); + }; + + currentState.viewAsAdmin = function() { + viewAsMode = false; + currentState.viewAsTeam = undefined; + currentState.updateViewAsPreference(); + $location.path("/admin/library/" + currentState.library.id); + }; + + currentState.updateViewAsPreference = function(user) { + // You cannot view as admin if you are not signed in + // You cannot view as admin if you are not an administrator + // You cannot view as admin if you are viewing as a student + if (user) { currentUser = user }; + currentState.showAdminFunctionality = currentUser + && currentUser.administrating.length + && !viewAsMode; + + return currentState.showAdminFunctionality; + }; + + currentState.setLibraryState = function() { + var id = $route.current.params.libraryId; + var resolves = {}; + + if (id && (!currentState.library || currentState.library.id != id)) { + currentState.library = Library.get({id: id}); + resolves.library = currentState.library; + currentState.modules = Module.query({library: id}); + resolves.modules = currentState.modules; + } + + if (!currentState.libraries) { + currentState.libraries = Library.query(); + resolves.libraries = currentState.libraries; + + resolves.libraries.$promise.then(function(libraries) { + currentState.library = currentState.library || libraries[0]; + currentState.modules = Module.query({library: libraries[0].libraryId}); + }); + } + + return $q.all(resolves); + } + + currentState.loadLibrary = function(id) { + if(id) { + currentState.library = Library.get({id: id}); + currentState.modules = Module.query({library: id}); + return currentState.library; + } + }; + + currentState.refreshLibrary = function() { + currentState.loadLibrary(currentState.library.id); + currentState.libraries = Library.query(); + }; + + return currentState; +}]); \ No newline at end of file diff --git a/assets/js/services/team_manager_service.js b/assets/js/services/team_manager_service.js new file mode 100644 index 0000000..ec6be14 --- /dev/null +++ b/assets/js/services/team_manager_service.js @@ -0,0 +1,35 @@ +app.factory("TeamManager", ["Team", "User", function(Team, User) { + var service = {}; + + service.addUserByName = function(username, team) { + User.UserFindBy.get({github: username}).$promise + .then(function(user) { + team.users.push(user); + team.$update(); + }, function(err) { + alert(userName + " is not a user yet. Go to /admin/user/create || /user/create."); + }); + }; + + service.removeUserById = function(userId, team) { + team.users = $scope.team.users.filter(function(v) { + if(v.id !== userId) { + return v; + } + }); + team.$update(); + }; + + service.addAdminByName = function(username, team) { + var userPromise = User.UserFindBy.get({github: userName}).$promise; + userPromise.then(function(user) { + team.users.push(user); + team.admins.push(user); + team.$update(); + }, function(err) { + alert(userName + " is not a user yet.\nGo to /admin/user/create || /user/create.") + }); + }; + + return service; +}]); \ No newline at end of file diff --git a/assets/js/templates/directives/admin_library_template.html b/assets/js/templates/directives/admin_library_template.html deleted file mode 100644 index 6b364b1..0000000 --- a/assets/js/templates/directives/admin_library_template.html +++ /dev/null @@ -1,23 +0,0 @@ -
-
-

Library View - SFC3

-
- -
-
-
- -
-
- -
-
-
-
- -
-
\ No newline at end of file diff --git a/assets/js/templates/directives/admin_module_template.html b/assets/js/templates/directives/admin_module_template.html deleted file mode 100644 index ea037f6..0000000 --- a/assets/js/templates/directives/admin_module_template.html +++ /dev/null @@ -1,31 +0,0 @@ -
-
-
-

Module View

-
-
-
- -
-
-
-
-
-
- -
-
- -
-
-
-
- -
- -
- diff --git a/assets/js/templates/directives/admin_sidebar_template.html b/assets/js/templates/directives/admin_sidebar.html similarity index 87% rename from assets/js/templates/directives/admin_sidebar_template.html rename to assets/js/templates/directives/admin_sidebar.html index 5607259..641125d 100644 --- a/assets/js/templates/directives/admin_sidebar_template.html +++ b/assets/js/templates/directives/admin_sidebar.html @@ -1,5 +1,5 @@
-
+

{{module.name}}

  • diff --git a/assets/js/templates/directives/admin_top_nav_template.html b/assets/js/templates/directives/admin_top_nav_template.html deleted file mode 100644 index b6dabaf..0000000 --- a/assets/js/templates/directives/admin_top_nav_template.html +++ /dev/null @@ -1,39 +0,0 @@ - \ No newline at end of file diff --git a/assets/js/templates/directives/admin_topbar.html b/assets/js/templates/directives/admin_topbar.html new file mode 100644 index 0000000..79c621e --- /dev/null +++ b/assets/js/templates/directives/admin_topbar.html @@ -0,0 +1,42 @@ + \ No newline at end of file diff --git a/assets/js/templates/directives/student_library_template.html b/assets/js/templates/directives/student_library_template.html deleted file mode 100644 index b84758d..0000000 --- a/assets/js/templates/directives/student_library_template.html +++ /dev/null @@ -1,17 +0,0 @@ -
    -
    -

    Library

    -
    -
    - -
    -
    \ No newline at end of file diff --git a/assets/js/templates/directives/student_module_template.html b/assets/js/templates/directives/student_module_template.html deleted file mode 100644 index 30e26fe..0000000 --- a/assets/js/templates/directives/student_module_template.html +++ /dev/null @@ -1,12 +0,0 @@ -
    -
    -

    {{module.name}}

    -
    -
    - -
    -
    \ No newline at end of file diff --git a/assets/js/templates/directives/student_sidebar_template.html b/assets/js/templates/directives/student_sidebar.html similarity index 87% rename from assets/js/templates/directives/student_sidebar_template.html rename to assets/js/templates/directives/student_sidebar.html index 3e9aec5..bc4d0f0 100644 --- a/assets/js/templates/directives/student_sidebar_template.html +++ b/assets/js/templates/directives/student_sidebar.html @@ -1,5 +1,5 @@
    -
    +

    {{module.name}}

    • diff --git a/assets/js/templates/directives/student_top_bar_template.html b/assets/js/templates/directives/student_top_bar_template.html deleted file mode 100644 index 2f60f4d..0000000 --- a/assets/js/templates/directives/student_top_bar_template.html +++ /dev/null @@ -1,24 +0,0 @@ - \ No newline at end of file diff --git a/assets/js/templates/directives/student_topbar.html b/assets/js/templates/directives/student_topbar.html new file mode 100644 index 0000000..75e75dd --- /dev/null +++ b/assets/js/templates/directives/student_topbar.html @@ -0,0 +1,32 @@ +
      + + + + + + + + + + Login + +
      diff --git a/assets/js/templates/views/admin_library.html b/assets/js/templates/views/admin_library.html index 07db03d..5628161 100644 --- a/assets/js/templates/views/admin_library.html +++ b/assets/js/templates/views/admin_library.html @@ -1 +1,23 @@ - \ No newline at end of file +
      +
      +

      Library View - SFC3

      +
      + +
      +
      +
      + +
      +
      + +
      +
      +
      +
      + +
      +
      \ No newline at end of file diff --git a/assets/js/templates/views/admin_module.html b/assets/js/templates/views/admin_module.html index 6ddd231..c547fce 100644 --- a/assets/js/templates/views/admin_module.html +++ b/assets/js/templates/views/admin_module.html @@ -1 +1,30 @@ - \ No newline at end of file +
      +
      +
      +

      Module View

      +
      +
      +
      + +
      +
      +
      +
      +
      +
      + +
      +
      + +
      +
      +
      + + +
      diff --git a/assets/js/templates/views/student_library.html b/assets/js/templates/views/student_library.html index afaddbe..826d4c3 100644 --- a/assets/js/templates/views/student_library.html +++ b/assets/js/templates/views/student_library.html @@ -1 +1,17 @@ - \ No newline at end of file +
      +
      +

      Library

      +
      +
      + +
      +
      \ No newline at end of file diff --git a/assets/js/templates/views/student_module.html b/assets/js/templates/views/student_module.html index 2eda7ef..9327728 100644 --- a/assets/js/templates/views/student_module.html +++ b/assets/js/templates/views/student_module.html @@ -1 +1,12 @@ - +
      +
      +

      {{module.name}}

      +
      +
      + +
      +
      \ No newline at end of file diff --git a/assets/js/templates/views/team.html b/assets/js/templates/views/team.html index 6748258..177386a 100644 --- a/assets/js/templates/views/team.html +++ b/assets/js/templates/views/team.html @@ -4,7 +4,7 @@

      Cohort: {{team.name}}

    -
    +
    @@ -16,7 +16,7 @@

    Cohort: {{team.name}}

    - +
    @@ -26,7 +26,7 @@

    Cohort: {{team.name}}

    -
    +
    @@ -41,10 +41,10 @@

    Cohort: {{team.name}}

    diff --git a/assets/styles/top_nav.css b/assets/styles/top_nav.css index 2c39171..e90751b 100644 --- a/assets/styles/top_nav.css +++ b/assets/styles/top_nav.css @@ -1,4 +1,8 @@ .nav-title { font-weight:bold; font-size: 1.4em; +} + +[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak { + display: none !important; } \ No newline at end of file diff --git a/views/layout.ejs b/views/layout.ejs index ca1d648..ae21f26 100644 --- a/views/layout.ejs +++ b/views/layout.ejs @@ -29,6 +29,10 @@ --> + + + + @@ -36,30 +40,27 @@ + +
    -
    -
    - -
    -
    - -
    -
    +
    -
    + + + +
    -
    -
    - -
    -
    - -
    -
    + +
    @@ -119,6 +120,38 @@ --> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    - {{user.name}} (admin) + {{user.name}} (admin) -