import ngModule from '../module'; class AclService { constructor($http) { this.$http = $http; } reset() { this.user = null; this.roles = null; } load() { return this.$http.get('VnUsers/acl').then(async res => { this.user = res.data.user; this.roles = {}; for (let role of res.data.roles) { if (role.role) this.roles[role.role.name] = true; } this.acls = {}; await this.$http.get('VnUsers/acls').then(res => { res.data.forEach(acl => { this.acls[acl.model] = this.acls[acl.model] || {}; this.acls[acl.model][acl.property] = this.acls[acl.model][acl.property] || {}; this.acls[acl.model][acl.property][acl.accessType] = true; }); }); }); } hasAnyACL(model, property, accessType) { const acls = this.acls[model]; if (acls) { for (const prop of ['*', property]) { const acl = acls[prop]; if (acl && (acl['*'] || acl[accessType])) return true; } } return false; } hasAny(roles) { if (this.roles) { for (let role of roles) { if (this.roles[role]) return true; } } return false; } } AclService.$inject = ['$http']; ngModule.service('aclService', AclService);