"use strict"; angular.module("vocabulary") .service("accessRestriction", ["$q", "adminReports", "loginAndContinueService", "exceptionService", "premadeApi", function ($q, adminReports, loginAndContinueService, exceptionService, premadeApi) { var teacherAuthorizationData = []; return { enforceRestriction: enforceRestriction, hasAccess: hasAccess, userIsLoggedIn: userIsLoggedIn, getTeacherAuthData: getTeacherAuthData }; function enforceRestriction(data, redirectUrl) { return enforceLoginRestriction(data, redirectUrl) .then(adminReports.alertIfReportsOnly); } function userIsLoggedIn() { return loginAndContinueService.isAuthorized(); } function hasAccess() { return userIsLoggedIn() && !adminReports.hasReportsOnlyAccess(); } function enforceLoginRestriction(data, redirectUrl) { return $q.when(loginAndContinueService.loginIfNotAuthorized(data, redirectUrl)) .then(function (resp) { if (!resp) { throw exceptionService.getAuthorizationException("NOT AUTHORIZED"); } }); } function getTeacherAuthData() { return ensureTeacherAuthIsInitialized() .then(function () { return teacherAuthorizationData; }); } function ensureTeacherAuthIsInitialized() { return teacherAuthIsInitialized() ? $q.resolve() : initializeTeacherAuth(); } function teacherAuthIsInitialized() { return Object.keys(teacherAuthorizationData).length > 0; } function initializeTeacherAuth(){ return premadeApi.teacherInfo() .then(function(AuthData){ teacherAuthorizationData = AuthData; }); } }]);