/* Copyright (c) 2020, 2023, The Unified Company. This code is part of Unify. This program is free software; you can redistribute it and/or modify it under the terms of the ESA Software Community License - Strong Copyleft LICENSE, as published by the ESA. See the ESA Software Community License - Strong Copyleft LICENSE, for more details. https://unifyjs.org */ import unify from '../unify/unify.js'; import userPermission from '../unify/userPermission.js'; import validator from '../unify/validator.js'; class visitor{ value = 2.0; label = "Member"; color = "black"; type = "userGroup"; } export default class permissionManager{ __className = "permissionManager"; permissions = new Array(); addPermission( user, type ) { var currentPermission = new userPermission( user, type, "allow" ); this.permissions.push( currentPermission ); } allow( user, type ) { // If the user is not signed in and this method has this.user as first argument // user == false, to prevent that everybody gets access: if( !user ) { return false; } this.addPermission( user, type ); //this.permissions[ user.id ] = currentPermission; // performance upgrade } callPermissionMethod( object ) { var clone = object.simpleClone(); if( object.type == "table" && !object.updated ) { object.get(); } object.permissionManager.permissions = new Array(); object.permissions = new Array(); // this is the working one object.permission( clone ); } noPermissionsWarning( object ) { if( object.permissionManager.permissions.length == 0 ) { if( object.debug ) { console.log("No permissions are set for this object, All request are rejected.."); } } } createVisitor( ) { var table = new global.user(); table.username.value = "Visitor"; table.id = 0; table.groups = new visitor(); return table; } isAllowed( user, type, object ) { if( !user ) { // todo: do this in the core user = this.createVisitor( ); } if( object.permission ) { this.callPermissionMethod( object ); this.noPermissionsWarning( object ); } var permission = new userPermission( user, type ); if( this.permissions.length == 0 ) { return false; } var isPermitted = this.checkPermissions( permission ); if( isPermitted ) { return true; } else { return false; } } checkPermissions( permissionB ) { for( var c = 0; c < this.permissions.length; c++ ) { var permission = this.permissions[c]; if( this.checkPermission( permission, permissionB ) ) { return true; } } return false; } checkCollectionPermission( permission, permissionB ) { var permissionA = new userPermission( permissionB.userObject, permission.type, permissionB.userObject ); if( this.comparePermission( permissionA, permissionB ) ) { return true; } /* var users = collection.querySelect(); for(var b = 0; b