/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.security.rbac.OperationType
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.Entity
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityHeaderSet
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.objectmodel.SaveException
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.objectmodel.folder.HasFolder
 *  org.jetbrains.annotations.NotNull
 *  org.jetbrains.annotations.Nullable
 */
package com.l7tech.server;

import com.l7tech.gateway.common.security.rbac.OperationType;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.Entity;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityHeaderSet;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.objectmodel.folder.HasFolder;
import com.l7tech.server.EntityCrud;
import com.l7tech.server.EntityHeaderUtils;
import com.l7tech.server.FolderSupportHibernateEntityManager;
import com.l7tech.server.security.rbac.RbacServices;
import com.l7tech.server.security.rbac.SecurityFilter;
import com.l7tech.server.security.rbac.ZoneUpdateSecurityChecker;
import com.l7tech.server.util.JaasUtils;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/*
 * Exception performing whole class analysis ignored.
 */
public class SecuredEntityCrud
implements EntityCrud {
    private static final String a;
    private final RbacServices b;
    private final SecurityFilter c;
    private final EntityCrud d;
    private final ZoneUpdateSecurityChecker e;
    private static final String[] g;

    public SecuredEntityCrud(RbacServices rbacServices, SecurityFilter securityFilter, EntityCrud entityCrud, @NotNull ZoneUpdateSecurityChecker zoneUpdateSecurityChecker) {
        if (zoneUpdateSecurityChecker == null) {
            SecuredEntityCrud.a(0);
        }
        this.b = rbacServices;
        this.c = securityFilter;
        this.d = entityCrud;
        this.e = zoneUpdateSecurityChecker;
    }

    @Override
    public void delete(Entity entity) throws DeleteException {
        this.a(OperationType.DELETE, entity, DeleteException.class);
        this.d.delete(entity);
    }

    @Override
    public void evict(Entity entity) {
        this.d.evict(entity);
    }

    @Override
    public <ET extends Entity> ET find(Class<ET> clazz, Serializable serializable) throws FindException {
        ET ET = this.d.find(clazz, serializable);
        this.a(OperationType.READ, (Entity)ET, (Class)FindException.class);
        return ET;
    }

    @Override
    public Entity find(@NotNull EntityHeader entityHeader) throws FindException {
        try {
            if (entityHeader == null) {
                SecuredEntityCrud.a(1);
            }
        }
        catch (FindException findException) {
            throw SecuredEntityCrud.a((Exception)((Object)findException));
        }
        Entity entity = this.d.find(entityHeader);
        this.a(OperationType.READ, entity, FindException.class);
        return entity;
    }

    @Override
    public EntityHeaderSet<EntityHeader> findAll(Class<? extends Entity> clazz) throws FindException {
        return this.c.filter(this.d.findAll(clazz), this.a(), OperationType.READ, (String)null);
    }

    @Override
    public EntityHeaderSet<EntityHeader> findAll(Class<? extends Entity> clazz, Map<String, String> map, int n2, int n3) throws FindException {
        return this.c.filter(this.d.findAll(clazz, map, n2, n3), this.a(), OperationType.READ, (String)null);
    }

    @Override
    public <ET extends Entity> List<ET> findAll(Class<ET> clazz, Map<String, List<Object>> map, int n2, int n3, Boolean bl2, String string) throws FindException {
        return this.c.filter(this.d.findAll(clazz, map, n2, n3, bl2, string), this.a(), OperationType.READ, null);
    }

    @Override
    public EntityHeaderSet<EntityHeader> findAllInScope(Class<? extends Entity> clazz, EntityHeader entityHeader, Map<String, String> map, int n2, int n3) throws FindException {
        return this.c.filter(this.d.findAllInScope(clazz, entityHeader, map, n2, n3), this.a(), OperationType.READ, (String)null);
    }

    @Override
    public EntityHeader findHeader(EntityType entityType, Serializable serializable) throws FindException {
        return this.d.findHeader(entityType, serializable);
    }

    @Override
    public Serializable save(Entity entity) throws SaveException {
        this.a(OperationType.CREATE, entity, SaveException.class);
        return this.d.save(entity);
    }

    @Override
    public void save(Goid goid, Entity entity) throws SaveException {
        this.a(OperationType.CREATE, entity, SaveException.class);
        this.d.save(goid, entity);
    }

    @Override
    public void update(Entity entity) throws UpdateException {
        this.a(OperationType.UPDATE, entity, UpdateException.class);
        this.d.update(entity);
    }

    public Collection<EntityHeader> findByEntityTypeAndSecurityZoneGoid(@NotNull EntityType entityType, Goid goid) throws FindException {
        try {
            if (entityType == null) {
                SecuredEntityCrud.a(2);
            }
        }
        catch (FindException findException) {
            throw SecuredEntityCrud.a((Exception)((Object)findException));
        }
        return this.c.filter(this.d.findByEntityTypeAndSecurityZoneGoid(entityType, goid), this.a(), OperationType.READ, null);
    }

    public void setSecurityZoneForEntities(@Nullable Goid goid, @NotNull EntityType entityType, @NotNull Collection<Serializable> collection) throws UpdateException {
        try {
            if (entityType == null) {
                SecuredEntityCrud.a(3);
            }
        }
        catch (FindException findException) {
            throw SecuredEntityCrud.a((Exception)((Object)findException));
        }
        try {
            if (collection == null) {
                SecuredEntityCrud.a(4);
            }
        }
        catch (FindException findException) {
            throw SecuredEntityCrud.a((Exception)((Object)findException));
        }
        try {
            this.e.checkBulkUpdatePermitted(this.a(), goid, entityType, collection);
            this.d.setSecurityZoneForEntities(goid, entityType, collection);
        }
        catch (FindException findException) {
            throw new UpdateException(g[10], (Throwable)findException);
        }
    }

    public void setSecurityZoneForEntities(@Nullable Goid goid, @NotNull Map<EntityType, Collection<Serializable>> map) throws UpdateException {
        try {
            if (map == null) {
                SecuredEntityCrud.a(5);
            }
        }
        catch (FindException findException) {
            throw SecuredEntityCrud.a((Exception)((Object)findException));
        }
        try {
            this.e.checkBulkUpdatePermitted(this.a(), goid, map);
            this.d.setSecurityZoneForEntities(goid, map);
        }
        catch (FindException findException) {
            throw new UpdateException(g[3], (Throwable)findException);
        }
    }

    /*
     * Loose catch block
     */
    private <T extends ObjectModelException> void a(OperationType operationType, Entity entity, Class<T> clazz) throws T {
        String[] stringArray;
        Object object;
        String string;
        block19: {
            try {
                if (entity == null) {
                    return;
                }
            }
            catch (FindException findException) {
                throw SecuredEntityCrud.a((Exception)((Object)findException));
            }
            string = null;
            try {
                block20: {
                    object = this.a();
                    if (!this.b.isPermittedForEntity((User)object, entity, operationType, null)) {
                        stringArray = g;
                        string = stringArray[6];
                        break block19;
                    }
                    if (!(entity instanceof HasFolder)) break block19;
                    if (OperationType.UPDATE != operationType) break block19;
                    break block20;
                    catch (FindException findException) {
                        throw SecuredEntityCrud.a((Exception)((Object)findException));
                    }
                }
                try {
                    block21: {
                        if (!FolderSupportHibernateEntityManager.changesExistingFolder((HasFolder)this.d.find(EntityHeaderUtils.fromEntity(entity)), (HasFolder)entity)) break block19;
                        break block21;
                        catch (FindException findException) {
                            throw SecuredEntityCrud.a((Exception)((Object)findException));
                        }
                    }
                    if (this.b.isPermittedForAnyEntityOfType((User)object, OperationType.DELETE, EntityType.FOLDER)) break block19;
                }
                catch (FindException findException) {
                    throw SecuredEntityCrud.a((Exception)((Object)findException));
                }
                stringArray = g;
                string = stringArray[13];
            }
            catch (FindException findException) {
                String[] stringArray2 = g;
                throw new RuntimeException(stringArray2[10], findException);
            }
        }
        if (string != null) {
            try {
                try {
                    object = clazz.getConstructor(String.class);
                    throw (ObjectModelException)((Object)((Constructor)object).newInstance(string));
                }
                catch (NoSuchMethodException noSuchMethodException) {
                    throw (ObjectModelException)((Object)clazz.newInstance());
                }
                catch (InvocationTargetException invocationTargetException) {
                    throw (ObjectModelException)((Object)clazz.newInstance());
                }
            }
            catch (IllegalAccessException illegalAccessException) {
                stringArray = g;
                throw new RuntimeException(stringArray[10], illegalAccessException);
            }
            catch (InstantiationException instantiationException) {
                throw new RuntimeException(g[10], instantiationException);
            }
        }
    }

    private User a() {
        return JaasUtils.getCurrentUser();
    }

    /*
     * Exception decompiling
     */
    private static /* synthetic */ void a(int var0) {
        /*
         * This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
         * 
         * org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [0[TRYBLOCK]], but top level block is 2[SWITCH]
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:435)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:484)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:736)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:850)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:278)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:201)
         *     at org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:94)
         *     at org.benf.cfr.reader.entities.Method.analyse(Method.java:531)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:1050)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:942)
         *     at org.benf.cfr.reader.Driver.doJarVersionTypes(Driver.java:257)
         *     at org.benf.cfr.reader.Driver.doJar(Driver.java:139)
         *     at org.benf.cfr.reader.CfrDriverImpl.analyse(CfrDriverImpl.java:76)
         *     at org.benf.cfr.reader.Main.main(Main.java:54)
         */
        throw new IllegalStateException("Decompilation failed");
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[15];
                var3_1 = 0;
                var2_2 = "?\u00170( R&>\u001b0\u0002\u001f^=)4+\t\u0000_'%\u0006-\u001e6>\r\u0000#\u000e(T=8R\"\u00147\u0011\u0013\u0002\u001d050]?l\u0002%\t$\\68\u00176[b\u0014 kR+\u001de\u0014 bW7[(D 8R*\u00141\u00111)R*\u000e)]\u0006p\u001b*\u00121\u000f\u001a\t\u00006\u00147\u0011:\"R4\u001e7\\:?\u0001-\u0014+\u00110$\u0017'\u0010k\u0004*\u001b*\u001f\u0006$\u0017%\u001f C\u0011\u001c\u00176\u0016,B %\u001d*[\u0001T=%\u0017 \n)\u001c0\u00121H\u00075\u0002!\u00196\u001d*\u001e\u0010A7-\u0006!( R&>\u001b0\u0002\u0006Y6/\u0019!\t#/\u001d)T)\u0006')\u0011,T6T!:\u00176T\u0016T09\u0000!\u001f\u0000_'%\u0006=87D7\u001a\t\u00006\u00147\u0011:\"R4\u001e7\\:?\u0001-\u0014+\u00110$\u0017'\u0010k#*\u001b*\u001f\u0007H\u0016\"\u0006-\u000f<e*<\u0017\u0005\u0015!b6/\u00076\u00121H\t#\u001c!<*X7\u00048\u000b4\u001e";
                var4_3 = "?\u00170( R&>\u001b0\u0002\u001f^=)4+\t\u0000_'%\u0006-\u001e6>\r\u0000#\u000e(T=8R\"\u00147\u0011\u0013\u0002\u001d050]?l\u0002%\t$\\68\u00176[b\u0014 kR+\u001de\u0014 bW7[(D 8R*\u00141\u00111)R*\u000e)]\u0006p\u001b*\u00121\u000f\u001a\t\u00006\u00147\u0011:\"R4\u001e7\\:?\u0001-\u0014+\u00110$\u0017'\u0010k\u0004*\u001b*\u001f\u0006$\u0017%\u001f C\u0011\u001c\u00176\u0016,B %\u001d*[\u0001T=%\u0017 \n)\u001c0\u00121H\u00075\u0002!\u00196\u001d*\u001e\u0010A7-\u0006!( R&>\u001b0\u0002\u0006Y6/\u0019!\t#/\u001d)T)\u0006')\u0011,T6T!:\u00176T\u0016T09\u0000!\u001f\u0000_'%\u0006=87D7\u001a\t\u00006\u00147\u0011:\"R4\u001e7\\:?\u0001-\u0014+\u00110$\u0017'\u0010k#*\u001b*\u001f\u0007H\u0016\"\u0006-\u000f<e*<\u0017\u0005\u0015!b6/\u00076\u00121H\t#\u001c!<*X7\u00048\u000b4\u001e".length();
                var1_4 = 26;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 96;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = SecuredEntityCrud.a(v0, SecuredEntityCrud.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "#4\u00016\tjZ0+\t3\u0018}Z!>\u0003;\t|\t\u00005\u0019;\u0018a3\u0001(";
                    var4_3 = "#4\u00016\tjZ0+\t3\u0018}Z!>\u0003;\t|\t\u00005\u0019;\u0018a3\u0001(".length();
                    var1_4 = 20;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 73;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = SecuredEntityCrud.a(v0, SecuredEntityCrud.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        SecuredEntityCrud.g = var5;
        SecuredEntityCrud.a = SecuredEntityCrud.g[10];
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x33);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 44;
                    case 1 -> 18;
                    case 2 -> 36;
                    case 3 -> 27;
                    case 4 -> 37;
                    case 5 -> 81;
                    default -> 51;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

