UNPKG

zettapi_client

Version:

Admin panel and client-side CRUD operations in angular to use with zettapi_server rest api to get started quickly in any CMS project.

174 lines (145 loc) 6.03 kB
app.factory('$auth', function ($http, $crypto, blockUI, $location, anonymousPages, $uibModalStack, customPages, routeDepth, $rootScope, Idle, zapi, $license) { var separator = '#/'; function getUrlPaths(url) { var start = url.indexOf(separator) + separator.length; var end = getUrlEnd(url, start); var shortURL = start >= separator.length ? (end >= 0 ? url.substring(start, end) : url.substring(start)) : ""; var urlPaths = shortURL.split('/'); if (!(urlPaths instanceof Array)) urlPaths = []; return urlPaths; } function getUrlEnd(url, start) { var querystring = url.indexOf('?'); if (querystring >= 0) { //using querystring return querystring; } else { //not using querystring var anchor = url.lastIndexOf('#'); if (anchor === start - separator.length) { //not using anchor return -1; } else { //using anchor return anchor; } } } var service = { currentUser: null, login: function (username, password) { return $http.post(zapi.serverUrl + '/api/session/login/', { username: username, password: $crypto.isMd5(password) ? password : $crypto.md5(password) }).then(function (response) { if (zapi.idle) Idle.watch(); service.currentUser = response.data.user; }); }, logout: function () { $http.get(zapi.serverUrl + '/api/session/logout').then(function (response) { if (zapi.idle) Idle.unwatch(); service.currentUser = null; $rootScope.login = {}; $location.path('/'); $uibModalStack.dismissAll(); }); }, requestCurrentUser: function (callback) { if (service.isAuthenticated()) return callback(null, service.currentUser); $http.get(zapi.serverUrl + '/api/session/currentuser').then(function (response) { service.currentUser = response.data.user; callback(null, service.currentUser); }).catch(function (response) { service.currentUser = null; $rootScope.login = {}; callback(response); }); }, isAuthenticated: function () { return !!service.currentUser; }, activateAccount: function (codes, callback) { return $http.post(zapi.serverUrl + '/api/user/activate/', { email: codes[0], code: codes[1] }); }, resetPassword: function (email, username, callback) { return $http.post(zapi.serverUrl + '/api/user/resetpassword/', { email: email, username: username }); }, changePassword: function (user, newPassword1, newPassword2, callback) { user.newPassword = $crypto.md5(newPassword1); return $http.post(zapi.serverUrl + '/api/user/changepassword/', user); }, isPageAnonymous: function (urlPaths) { if (!urlPaths) urlPaths = getUrlPaths($location.path()); return anonymousPages.some(function (anonymousPage) { return anonymousPage === urlPaths[0]; }); }, isPageCustom: function (urlPaths) { if (!urlPaths) urlPaths = getUrlPaths($location.path()); return customPages.find(function (customPage) { if (customPage.urlParts.length < urlPaths.length) return false; return !customPage.urlParts.some(function (part, i) { //ignore url wildcards by configuring the custom page with % if (part === '%') return false; return part !== urlPaths[i]; }); }); }, getNextRoute: function (callback) { return function (event, next, current) { var urlPaths = getUrlPaths(next); if (urlPaths.length === 0) return callback(); if (service.isPageAnonymous(urlPaths)) return callback(); //service.currentUser = null; service.requestCurrentUser(function (err, user) { $rootScope.login = (err ? {} : user) || {}; if (!$rootScope.login._id) return callback("Sem sessão iniciada. Isto pode ocorrer por motivos de inactividade, aceder à mesma conta noutro computador ou actualização recente ao servidor.", user); if (urlPaths[0] === 'profile') return callback(null, user); //authenticate and authorize custom pages var customPage = service.isPageCustom(urlPaths); if (customPage) { //check license if (typeof customPage.license !== 'undefined') { if (!$license.isLicensed(customPage.license)) $license.notify(customPage.license); } return callback(null, user); } //authenticate and authorize entity pages var entity = zapi.entityMap[urlPaths[routeDepth]]; if (!entity) return callback("A página que está a tentar consultar não existe", user); if (urlPaths.length <= routeDepth + 1) return callback("A página que está a tentar consultar não existe", user); var action = entity[urlPaths[routeDepth + 1]]; if (!action) return callback("A página que está a tentar consultar não existe", user); if (user.role.admin) return callback(null, user); if (action.admin) return callback("A página que está a tentar consultar não existe", user); //check license if (typeof entity.license !== 'undefined') { if (!$license.isLicensed(entity.license)) $license.notify(entity.license); } return callback(null, user); }); }; }, isAnonymous: function () { return !service.isLoggedIn(); }, isLoggedIn: function () { if (!service.currentUser) return false; return !!service.currentUser._id; }, isAdmin: function () { if (service.isLoggedIn()) return service.currentUser.role.admin; return false; }, hasClearance: function (level) { level = level || 0; if (!service.currentUser) return false; if (!service.currentUser.role) return false; return service.currentUser.role.approvalLevel >= level; } }; return service; });