class UserManager extends Listenable { constructor() { super(); this.users = { }; this.roles = { }; this.loadUsersRequest = new Request(TimeCards.REQUEST_URI + "Users", Request.GET, this.loadedUsers.bind(this), this.userLoadingError.bind(this)); this.loadUsersInterval = setInterval(this.loadUsers.bind(this), 60000); this.loadUsers(); this.deleteUserRequest = new Request(TimeCards.REQUEST_URI + "User/Delete", Request.POST, this.deletedUser.bind(this), this.userDeletionError.bind(this)); this.createUserRequest = new Request(TimeCards.REQUEST_URI + "User/Create", Request.POST, this.createdUser.bind(this), this.userCreationError.bind(this)); this.updateUserRequest = new Request(TimeCards.REQUEST_URI + "User", Request.POST, this.updatedUser.bind(this), this.userUpdatingError.bind(this)); this.deleteRoleRequest = new Request(TimeCards.REQUEST_URI + "Role/Delete", Request.POST, this.deletedRole.bind(this), this.roleDeletionError.bind(this)); this.createRoleRequest = new Request(TimeCards.REQUEST_URI + "Role/Create", Request.POST, this.createdRole.bind(this), this.roleCreationError.bind(this)); this.updateRoleRequest = new Request(TimeCards.REQUEST_URI + "Role", Request.POST, this.updatedRole.bind(this), this.roleUpdatingError.bind(this)); } performInitialCalls(eventType, callback) { if (eventType == "roleadded") { for (var roleId in this.roles) { var addedRole = this.roles[roleId]; var roleAddedEvent = new Event("roleadded"); roleAddedEvent.role = addedRole; callback(roleAddedEvent); } } else if (eventType == "useradded") { for (var userId in this.users) { var addedUser = this.users[userId]; var userAddedEvent = new Event("useradded"); userAddedEvent.user = addedUser; callback(userAddedEvent); } } } /** * Shows or hides the elements on the page depending on whether the user has access to them or not. * This is done via CSS classes. */ applyPermissionsToInterface() { //Do nothing if there is no logged in user. if (!this.sessionUser) { return; } var allPermissions = [ "accounts", "user_role", "user_role_grant", "user", "user_samerole", "user_all", "report_user", "card", "card_category", "project", "project_all", "project_assigned", "report_project", "period", "period_all", "multiuser", "project_user", "project_user_tariff" ]; var permissions = this.roles[this.sessionUser.id_role].permissions; //Re-enable all elements that were be disabled because of the lack of permission in the last update. if (this.elementsDisabledBecauseOfLackingPermissions) { for (var i = 0; i < this.elementsDisabledBecauseOfLackingPermissions.length; i++) { if (this.elementsDisabledBecauseOfLackingPermissions[i].hasAttribute("disabled")) { this.elementsDisabledBecauseOfLackingPermissions[i].removeAttribute("disabled"); } } } //Disable all elements that this user has no access to. this.elementsDisabledBecauseOfLackingPermissions = [ ]; for (var i = 0; i < allPermissions.length; i++) { var permission = allPermissions[i]; var readElements = document.body.querySelectorAll(".available__" + permission); var writeElements = document.body.querySelectorAll(".available_w__" + permission); if (!permission in permissions) { for (var j = 0; j < readElements.length; j++) { this.elementsDisabledBecauseOfLackingPermissions.push(readElements[j]); } } if ((!permission in permissions) || permissions[permission] == "r") { for (var j = 0; j < writeElements.length; j++) { this.elementsDisabledBecauseOfLackingPermissions.push(writeElements[j]); } } } for (var i = 0; i < this.elementsDisabledBecauseOfLackingPermissions.length; i++) { this.elementsDisabledBecauseOfLackingPermissions[i].setAttribute("disabled", "true"); } for (var permission in permissions) { document.body.classList.add("enable__" + permission); if (permissions[permission] == "w") { document.body.classList.add("enable_w__" + permission); } } } /** * Returns the permissions of the logged in user. */ getPermissions() { if (!this.sessionUser) { return { }; } return this.roles[this.sessionUser.id_role].permissions; } /** * Returns the user with the given ID from this account. * Will return null if the user was not found. */ getUser(userId) { if (!this.users || !(userId in this.users)) { return null; } return this.users[userId]; } /** * Checks whether the logged in user has the required access ("w" or "r") on the given resource or not and returns this information. * @return true if the resource is available to the user, false if not. */ isResourceAvailable(permission, type) { var permissions = this.getPermissions(); if (permission in permissions && (permissions[permission] == type || (type == "r" && permissions[permission] == "w"))) { return true; } return false; } loadUsers() { if (this.loadUsersRequest.ready) { this.loadUsersRequest.send(); } } loadedUsers(roles) { //Check for deleted and updated roles. for (var roleId in this.roles) { if (!(roleId in roles)) { //The role does no longer exist. var roleRemovedEvent = new Event("roleremoved"); roleRemovedEvent.role = this.roles[roleId]; this.dispatchEvent(roleRemovedEvent); continue; } if (!Object.equals(roles[roleId], this.roles[roleId])) { //The role has changed. var roleChangedEvent = new Event("rolechanged"); roleChangedEvent.role = roles[roleId]; this.dispatchEvent(roleChangedEvent); } } //Process new roles. for (var roleId in roles) { if (!(roleId in this.roles)) { var roleAddedEvent = new Event("roleadded"); roleAddedEvent.role = roles[roleId]; this.dispatchEvent(roleAddedEvent); } } this.roles = roles; //Collect all users and process them as well. var users = { }; for (var roleId in roles) { for (var userId in roles[roleId].users) { users[userId] = roles[roleId].users[userId]; if (users[userId].is_session_user) { this.sessionUser = users[userId]; //Apply the new permissions if they have changed. if (!Object.equals(this.permissions, this.roles[this.sessionUser.id_role].permissions)) { this.permissions = this.roles[this.sessionUser.id_role].permissions; this.applyPermissionsToInterface(); } } } } //Check for deleted and updated users. for (var userId in this.users) { if (!(userId in users)) { //The user does no longer exist. var userRemovedEvent = new Event("userremoved"); userRemovedEvent.user = this.users[userId]; this.dispatchEvent(userRemovedEvent); continue; } if (!Object.equals(users[userId], this.users[userId])) { //The user has changed. var userChangedEvent = new Event("userchanged"); userChangedEvent.user = users[userId]; this.dispatchEvent(userChangedEvent); } } //Process new users. for (var userId in users) { if (!(userId in this.users)) { var userAddedEvent = new Event("useradded"); userAddedEvent.user = users[userId]; this.dispatchEvent(userAddedEvent); } } this.users = users; } userLoadingError(request, status, error) { alert("Error during the loading of the users:\r\n\r\n" + JSON.stringify(error, true)); } /** * Attempts to delete the given user on the server side. */ deleteUser(user) { this.deleteUserRequest.send(user); } deletedUser(response) { var userRemovedEvent = new Event("userremoved"); userRemovedEvent.user = response; this.dispatchEvent(userRemovedEvent); //Log out if the currently logged in user was deleted. if (response.user_id == this.sessionUser.user_id) { this.sessionUser = null; localStorage.removeItem("session"); } delete this.users[response.user_id]; } userDeletionError(request, status, error) { alert("Error during the deletion of the user:\r\n\r\n" + JSON.stringify(error, true)); } /** * Attempts to create a user with the given data on the server side. */ createUser(user) { this.createUserRequest.send(user); } createdUser(response) { this.users[response.user_id] = response; this.roles[response.id_role].users[response.user_id] = response; var userAddedEvent = new Event("useradded"); userAddedEvent.user = response; this.dispatchEvent(userAddedEvent); } userCreationError(request, status, error) { alert("Error during the creation of the user:\r\n\r\n" + JSON.stringify(error, true)); } /** * Attempts to save the given user data on the server. */ updateUser(user) { this.updateUserRequest.send(user); } updatedUser(response) { delete this.users[response.user_id]; this.users[response.user_id] = response; var userChangedEvent = new Event("userchanged"); userChangedEvent.user = response; this.dispatchEvent(userChangedEvent); } userUpdatingError(request, status, error) { alert("Error while updating the user:\r\n\r\n" + JSON.stringify(error, true)); } /** * Attempts to delete the given role on the server side. */ deleteRole(role) { this.deleteRoleRequest.send(role); } deletedRole(response) { var roleRemovedEvent = new Event("roleremoved"); roleRemovedEvent.role = response; this.dispatchEvent(roleRemovedEvent); delete this.roles[response.id_role]; } roleDeletionError(request, status, error) { alert("Error during the deletion of the role:\r\n\r\n" + JSON.stringify(error, true)); } /** * Attempts to create a role with the given data on the server side. */ createRole(role) { this.createRoleRequest.send(role); } createdRole(response) { this.roles[response.id_role] = response; var roleAddedEvent = new Event("roleadded"); roleAddedEvent.role = response; this.dispatchEvent(roleAddedEvent); } roleCreationError(request, status, error) { alert("Error during the creation of the role:\r\n\r\n" + JSON.stringify(error, true)); } /** * Attempts to save the given role data on the server. */ updateRole(role) { this.updateRoleRequest.send(role); } updatedRole(response) { this.roles[response.id_role] = response; var roleChangedEvent = new Event("rolechanged"); roleChangedEvent.role = response; this.dispatchEvent(roleChangedEvent); } roleUpdatingError(request, status, error) { alert("Error while updating the role:\r\n\r\n" + JSON.stringify(error, true)); } }