11import { BadRequestError , PermissionError } from '../exceptions' ;
22import { EnvType } from '../models/environment' ;
3- import { ActionTypes , RouterTypes } from '../models/permission' ;
43import { getDomainById } from '../services/domain' ;
54import { getEnvironments } from '../services/environment' ;
65import { getTeams } from '../services/team' ;
7- import { getPermission , getPermissions } from '../services /permission' ;
6+ import { verifyPermissions , verifyPermissionsCascade } from './permission' ;
87
98export async function checkEnvironmentStatusRemoval ( domainId , environmentName , strategy = false ) {
109 const environment = await getEnvironments ( { domain : domainId } , [ '_id' , 'name' ] ) ;
@@ -41,6 +40,10 @@ export function parseJSON(str) {
4140 }
4241}
4342
43+ export function containsValue ( arr , value ) {
44+ return arr ?. filter ( item => item . match ( value ) ) . length > 0 ;
45+ }
46+
4447export function formatInput ( input ,
4548 options = {
4649 toUpper : false ,
@@ -105,79 +108,4 @@ export async function verifyOwnership(admin, element, domainId, action, routerTy
105108 }
106109
107110 return element ;
108- }
109-
110- async function verifyPermissions ( team , element , action , routerType ) {
111- const permission = await getPermission ( {
112- _id : { $in : team . permissions } ,
113- action : { $in : [ action , ActionTypes . ALL ] } ,
114- active : true ,
115- router : { $in : [ routerType , RouterTypes . ALL ] }
116- } ) ;
117-
118- if ( permission ) {
119- return verifyIdentifiers ( permission , element ) ;
120- } else {
121- throw new PermissionError ( `Permission not found for this operation: '${ action } ' - '${ routerType } '` ) ;
122- }
123- }
124-
125- async function verifyPermissionsCascade ( team , element , action , routerType ) {
126- let orStatement = [ ] ;
127- if ( routerType === RouterTypes . DOMAIN ) {
128- orStatement = [
129- { router : routerType } ,
130- { router : RouterTypes . GROUP } ,
131- { router : RouterTypes . CONFIG } ,
132- { router : RouterTypes . STRATEGY } ,
133- { router : RouterTypes . ALL }
134- ] ;
135- } else if ( routerType === RouterTypes . GROUP ) {
136- orStatement = [
137- { router : routerType } ,
138- { router : RouterTypes . CONFIG } ,
139- { router : RouterTypes . STRATEGY } ,
140- { router : RouterTypes . ALL }
141- ] ;
142- } else if ( routerType === RouterTypes . CONFIG || routerType === RouterTypes . STRATEGY ) {
143- orStatement = [
144- { router : routerType } ,
145- { router : RouterTypes . STRATEGY } ,
146- { router : RouterTypes . ALL }
147- ] ;
148- }
149-
150- const foundPermission = await getPermissions ( {
151- _id : { $in : team . permissions } ,
152- action : { $in : [ action , ActionTypes . ALL ] } ,
153- active : true ,
154- $or : orStatement
155- } ) ;
156-
157- const matchedPermission = foundPermission . filter ( value => value . router === routerType ) ;
158- if ( matchedPermission . length ) {
159- return verifyIdentifiers ( matchedPermission [ 0 ] , element ) ;
160- } else if ( foundPermission [ 0 ] ) {
161- return element ;
162- }
163- }
164-
165- function verifyIdentifiers ( permission , element ) {
166- if ( permission . identifiedBy ) {
167- if ( Array . isArray ( element ) ) {
168- if ( permission . values . length ) {
169- element = element . filter ( child => permission . values . includes ( child [ `${ permission . identifiedBy } ` ] ) ) ;
170- if ( element . length ) {
171- return element ;
172- }
173- }
174- } else {
175- if ( permission . values . includes ( element [ `${ permission . identifiedBy } ` ] ) ) {
176- return element ;
177- }
178- }
179- } else {
180- return element ;
181- }
182- throw new PermissionError ( 'It was not possible to match the requiring element to the current permission' ) ;
183111}
0 commit comments