/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.security.rbac.ObjectIdentityPredicate
 *  com.l7tech.gateway.common.security.rbac.OperationType
 *  com.l7tech.gateway.common.security.rbac.Permission
 *  com.l7tech.gateway.common.security.rbac.Role
 *  com.l7tech.gateway.common.security.rbac.ScopePredicate
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.gateway.common.service.ServiceHeader
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.folder.Folder
 *  com.l7tech.objectmodel.folder.FolderHeader
 *  com.l7tech.policy.Policy
 *  com.l7tech.policy.PolicyHeader
 *  com.l7tech.policy.PolicyType
 *  com.l7tech.util.Triple
 *  org.jetbrains.annotations.NotNull
 *  org.springframework.beans.BeansException
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.upgrade;

import com.l7tech.gateway.common.security.rbac.ObjectIdentityPredicate;
import com.l7tech.gateway.common.security.rbac.OperationType;
import com.l7tech.gateway.common.security.rbac.Permission;
import com.l7tech.gateway.common.security.rbac.Role;
import com.l7tech.gateway.common.security.rbac.ScopePredicate;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.gateway.common.service.ServiceHeader;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.folder.Folder;
import com.l7tech.objectmodel.folder.FolderHeader;
import com.l7tech.policy.Policy;
import com.l7tech.policy.PolicyHeader;
import com.l7tech.policy.PolicyType;
import com.l7tech.server.folder.FolderManager;
import com.l7tech.server.policy.PolicyManager;
import com.l7tech.server.security.rbac.RoleManager;
import com.l7tech.server.service.ServiceManager;
import com.l7tech.server.upgrade.FatalUpgradeException;
import com.l7tech.server.upgrade.NonfatalUpgradeException;
import com.l7tech.server.upgrade.UpgradeTask;
import com.l7tech.util.Triple;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

public abstract class AbstractDynamicRolePermissionsUpgradeTask
implements UpgradeTask {
    private static final Logger a;
    private ApplicationContext b;
    protected ServiceManager serviceManager;
    private static final String[] d;

    @Override
    public void upgrade(ApplicationContext applicationContext) throws FatalUpgradeException, NonfatalUpgradeException {
        this.b = applicationContext;
        String[] stringArray = d;
        PolicyManager policyManager = this.a(stringArray[8], PolicyManager.class);
        this.serviceManager = this.a(stringArray[19], ServiceManager.class);
        RoleManager roleManager = this.a(stringArray[20], RoleManager.class);
        FolderManager folderManager = this.a(stringArray[1], FolderManager.class);
        try {
            try {
                if (this.getEntityTypesToUpgrade().contains(EntityType.POLICY)) {
                    this.a(policyManager, roleManager);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            try {
                if (this.getEntityTypesToUpgrade().contains(EntityType.SERVICE)) {
                    this.a(this.serviceManager, roleManager);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            try {
                if (this.getEntityTypesToUpgrade().contains(EntityType.FOLDER)) {
                    this.a(folderManager, roleManager);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
        }
        catch (ObjectModelException objectModelException) {
            throw new NonfatalUpgradeException(objectModelException);
        }
    }

    private <T> T a(String string, Class<T> clazz) throws FatalUpgradeException {
        try {
            if (this.b == null) {
                throw new FatalUpgradeException(d[4]);
            }
        }
        catch (BeansException beansException) {
            throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
        }
        try {
            return (T)this.b.getBean(string, clazz);
        }
        catch (BeansException beansException) {
            String[] stringArray = d;
            throw new FatalUpgradeException(stringArray[13] + string + stringArray[5]);
        }
    }

    private void a(PolicyManager policyManager, RoleManager roleManager) throws ObjectModelException {
        List<Triple<OperationType, String, EntityType>> list = this.permissionsToAdd();
        Collection collection = policyManager.findAllHeaders();
        for (PolicyHeader policyHeader : collection) {
            Goid goid = policyHeader.getGoid();
            PolicyType policyType = policyHeader.getPolicyType();
            try {
                if (this.shouldIgnorePolicyType(policyType)) {
                    continue;
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            boolean bl2 = false;
            for (Triple<OperationType, String, EntityType> triple : list) {
                bl2 = this.a(roleManager, EntityType.POLICY, goid, (OperationType)triple.left, (String)triple.middle, (EntityType)triple.right);
                try {
                    if (!bl2) continue;
                    break;
                }
                catch (ObjectModelException objectModelException) {
                    throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                }
            }
            try {
                if (!bl2 || !this.shouldCreateMissingRoles()) continue;
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            Policy policy = (Policy)policyManager.findByPrimaryKey(goid);
            try {
                if (policy != null) {
                    a.warning(d[16] + String.valueOf(goid) + d[12]);
                    policyManager.createRoles((PersistentEntity)policy);
                    continue;
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            String[] stringArray = d;
            a.warning(stringArray[18] + String.valueOf(goid) + stringArray[21]);
        }
    }

    private void a(ServiceManager serviceManager, RoleManager roleManager) throws ObjectModelException {
        List<Triple<OperationType, String, EntityType>> list = this.permissionsToAdd();
        Collection<ServiceHeader> collection = serviceManager.findAllHeaders(false);
        for (ServiceHeader serviceHeader : collection) {
            Goid goid = serviceHeader.getGoid();
            boolean bl2 = false;
            for (Triple<OperationType, String, EntityType> triple : list) {
                bl2 = this.a(roleManager, EntityType.SERVICE, goid, (OperationType)triple.left, (String)triple.middle, (EntityType)triple.right);
                try {
                    if (!bl2) continue;
                    break;
                }
                catch (ObjectModelException objectModelException) {
                    throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                }
            }
            try {
                if (!bl2 || !this.shouldCreateMissingRoles()) continue;
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            PublishedService publishedService = (PublishedService)serviceManager.findByPrimaryKey(goid);
            try {
                if (publishedService != null) {
                    a.warning(d[7] + String.valueOf(goid) + d[9]);
                    serviceManager.createRoles((PersistentEntity)publishedService);
                    continue;
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            String[] stringArray = d;
            a.warning(stringArray[3] + String.valueOf(goid) + stringArray[17]);
        }
    }

    private void a(FolderManager folderManager, RoleManager roleManager) throws ObjectModelException {
        List<Triple<OperationType, String, EntityType>> list = this.permissionsToAdd();
        Collection collection = folderManager.findAllHeaders();
        for (FolderHeader folderHeader : collection) {
            Goid goid = folderHeader.getGoid();
            boolean bl2 = false;
            for (Triple<OperationType, String, EntityType> triple : list) {
                bl2 = this.a(roleManager, EntityType.FOLDER, goid, (OperationType)triple.left, (String)triple.middle, (EntityType)triple.right, 2);
                try {
                    if (!bl2) continue;
                    break;
                }
                catch (ObjectModelException objectModelException) {
                    throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                }
            }
            try {
                if (!bl2 || !this.shouldCreateMissingRoles()) continue;
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            Folder folder = (Folder)folderManager.findByPrimaryKey(goid);
            try {
                if (folder != null) {
                    a.warning(d[10] + String.valueOf(goid) + d[12]);
                    folderManager.createRoles((PersistentEntity)folder);
                    continue;
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
            }
            String[] stringArray = d;
            a.warning(stringArray[6] + String.valueOf(goid) + stringArray[14]);
        }
    }

    protected abstract List<Triple<OperationType, String, EntityType>> permissionsToAdd();

    protected abstract boolean shouldCreateMissingRoles();

    protected abstract boolean shouldIgnorePolicyType(PolicyType var1);

    @NotNull
    protected abstract Collection<EntityType> getEntityTypesToUpgrade();

    private boolean a(RoleManager roleManager, EntityType entityType, Goid goid, OperationType operationType, String string, EntityType entityType2) throws ObjectModelException {
        return this.a(roleManager, entityType, goid, operationType, string, entityType2, 1);
    }

    protected String getScopeId(EntityType entityType, Goid goid) throws ObjectModelException {
        return null;
    }

    private boolean a(RoleManager roleManager, EntityType entityType, Goid goid, OperationType operationType, String string, EntityType entityType2, int n2) throws ObjectModelException {
        boolean bl2 = false;
        Collection<Role> collection = roleManager.findEntitySpecificRoles(entityType, goid);
        if (collection.isEmpty()) {
            bl2 = true;
        } else if (collection.size() == n2) {
            for (Role role : collection) {
                try {
                    if (!this.a(role, operationType, string, entityType2)) continue;
                    roleManager.update((PersistentEntity)role);
                }
                catch (ObjectModelException objectModelException) {
                    throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                }
            }
        } else {
            int n3 = collection.size();
            int n4 = n2;
            String string2 = String.valueOf(goid);
            String string3 = entityType.getName();
            String[] stringArray = d;
            a.warning(stringArray[2] + string3 + stringArray[15] + string2 + stringArray[0] + n4 + stringArray[11] + n3 + ".");
        }
        return bl2;
    }

    private boolean a(Role role, OperationType operationType, String string, EntityType entityType) throws ObjectModelException {
        boolean bl2;
        boolean bl3;
        block13: {
            String string2;
            block14: {
                bl3 = false;
                string2 = this.getScopeId(role.getEntityType(), role.getEntityGoid());
                for (Permission permission : role.getPermissions()) {
                    try {
                        try {
                            try {
                                if (permission.getEntityType() != entityType || permission.getOperation() != operationType) continue;
                            }
                            catch (ObjectModelException objectModelException) {
                                throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                            }
                            if (!this.a(permission.getScope(), string2)) continue;
                        }
                        catch (ObjectModelException objectModelException) {
                            throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                        }
                        if (!this.a(permission.getOtherOperationName(), string)) continue;
                    }
                    catch (ObjectModelException objectModelException) {
                        throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                    }
                    bl3 = true;
                    break;
                }
                try {
                    try {
                        if (bl3) break block13;
                        if (!operationType.equals((Object)OperationType.OTHER)) break block14;
                    }
                    catch (ObjectModelException objectModelException) {
                        throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                    }
                    role.addEntityOtherPermission(entityType, string2, string);
                    break block13;
                }
                catch (ObjectModelException objectModelException) {
                    throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
                }
            }
            role.addEntityPermission(operationType, entityType, string2);
        }
        try {
            bl2 = !bl3;
        }
        catch (ObjectModelException objectModelException) {
            throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)objectModelException));
        }
        return bl2;
    }

    private boolean a(Set<ScopePredicate> set, String string) {
        boolean bl2;
        block12: {
            ScopePredicate scopePredicate;
            block8: {
                boolean bl3;
                block11: {
                    block10: {
                        try {
                            block9: {
                                try {
                                    try {
                                        try {
                                            if (string != null && !string.isEmpty()) break block8;
                                        }
                                        catch (BeansException beansException) {
                                            throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                                        }
                                        if (set == null) break block9;
                                    }
                                    catch (BeansException beansException) {
                                        throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                                    }
                                    if (!set.isEmpty()) break block10;
                                }
                                catch (BeansException beansException) {
                                    throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                                }
                            }
                            bl3 = true;
                            break block11;
                        }
                        catch (BeansException beansException) {
                            throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                        }
                    }
                    bl3 = false;
                }
                bl2 = bl3;
                break block12;
            }
            bl2 = set.size() != 1 ? false : ((scopePredicate = set.iterator().next()) instanceof ObjectIdentityPredicate ? ((ObjectIdentityPredicate)scopePredicate).getTargetEntityId().equals(string) : false);
        }
        return bl2;
    }

    private boolean a(String string, String string2) {
        boolean bl2;
        block16: {
            boolean bl3;
            block15: {
                block14: {
                    block10: {
                        boolean bl4;
                        block13: {
                            block12: {
                                block11: {
                                    try {
                                        if (string == null) break block10;
                                        if (string.isEmpty()) break block11;
                                    }
                                    catch (BeansException beansException) {
                                        throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                                    }
                                    bl2 = string.equals(string2);
                                    break block16;
                                }
                                try {
                                    try {
                                        if (string2 != null && !string2.isEmpty()) break block12;
                                    }
                                    catch (BeansException beansException) {
                                        throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                                    }
                                    bl4 = true;
                                    break block13;
                                }
                                catch (BeansException beansException) {
                                    throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                                }
                            }
                            bl4 = false;
                        }
                        bl2 = bl4;
                        break block16;
                    }
                    try {
                        try {
                            if (string2 != null && !string2.isEmpty()) break block14;
                        }
                        catch (BeansException beansException) {
                            throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                        }
                        bl3 = true;
                        break block15;
                    }
                    catch (BeansException beansException) {
                        throw AbstractDynamicRolePermissionsUpgradeTask.a((Exception)((Object)beansException));
                    }
                }
                bl3 = false;
            }
            bl2 = bl3;
        }
        return bl2;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[22];
                var3_1 = 0;
                var2_2 = "yrI1n2,=*\f06\r81\u00050s0\u0004?0\b3s0\u0018\u00101\u001dtc2.,?\r=x%i,1\u00051eb/1,I\u001a\u00137\u001a'\u007f,.~,\u00068sb/1,I's0?7=\ft1\u001e\u001f.\u00198\u007f!(*7\u0006:U-'*;\u0011 6+:~,\f%c+;;:\u001ay~\u000f&y/i\u001f.\u00198\u007f!(*7\u0006:U-'*;\u0011 8\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I2y.-;,Is\u001a\u00137\u001a'\u007f,.~,\u00068sb/1,I's0?7=\ft1\r.1\u0005=u;\u0004?0\b3s0\u0015yrI7d'(*7\u000736,,)~\u001b;z'g\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I2y.-;,Is\u0013~,\u00068sj:w~\u000b!bb/1+\u000706\u0015yrI7d'(*7\u000736,,)~\u001b;z'g\u0017\u001b,\u001b;db(==\f'e+'9~I6s#'~y3yrI:y6i=,\f5b+'9~\u00071ab;12\ft>7'?<\u0005166&~?\n7s1:~8\u00068r';wp\u0002~y\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I$y. ='Is4yrI:y6i=,\f5b+'9~\u00071ab;12\ft>7'?<\u0005166&~?\n7s1:~-\f&`+*;wG\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I$y. ='Is\u000e-;\u001b\"\u007f!,\u0013?\u00075q';";
                var4_3 = "yrI1n2,=*\f06\r81\u00050s0\u0004?0\b3s0\u0018\u00101\u001dtc2.,?\r=x%i,1\u00051eb/1,I\u001a\u00137\u001a'\u007f,.~,\u00068sb/1,I's0?7=\ft1\u001e\u001f.\u00198\u007f!(*7\u0006:U-'*;\u0011 6+:~,\f%c+;;:\u001ay~\u000f&y/i\u001f.\u00198\u007f!(*7\u0006:U-'*;\u0011 8\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I2y.-;,Is\u001a\u00137\u001a'\u007f,.~,\u00068sb/1,I's0?7=\ft1\r.1\u0005=u;\u0004?0\b3s0\u0015yrI7d'(*7\u000736,,)~\u001b;z'g\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I2y.-;,Is\u0013~,\u00068sj:w~\u000b!bb/1+\u000706\u0015yrI7d'(*7\u000736,,)~\u001b;z'g\u0017\u001b,\u001b;db(==\f'e+'9~I6s#'~y3yrI:y6i=,\f5b+'9~\u00071ab;12\ft>7'?<\u0005166&~?\n7s1:~8\u00068r';wp\u0002~y\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I$y. ='Is4yrI:y6i=,\f5b+'9~\u00071ab;12\ft>7'?<\u0005166&~?\n7s1:~-\f&`+*;wG\u0019\u00137\u001a'\u007f,.~,\u00068sb/1,I$y. ='Is\u000e-;\u001b\"\u007f!,\u0013?\u00075q';".length();
                var1_4 = 12;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = AbstractDynamicRolePermissionsUpgradeTask.a(v0, AbstractDynamicRolePermissionsUpgradeTask.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = ";&\u0012&L40(.\u001b13ne^-n!~*;\u001b\"u<0.i\u0010&vu,&%\u001bc) 0(+\u0012&!!1i(\u001d d&-i9\u0011/h6'`g";
                    var4_3 = ";&\u0012&L40(.\u001b13ne^-n!~*;\u001b\"u<0.i\u0010&vu,&%\u001bc) 0(+\u0012&!!1i(\u001d d&-i9\u0011/h6'`g".length();
                    var1_4 = 11;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 46;
                        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++] = AbstractDynamicRolePermissionsUpgradeTask.a(v0, AbstractDynamicRolePermissionsUpgradeTask.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;
            }
        }
        AbstractDynamicRolePermissionsUpgradeTask.d = var5;
        AbstractDynamicRolePermissionsUpgradeTask.a = Logger.getLogger(AbstractDynamicRolePermissionsUpgradeTask.class.getName());
    }

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

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x70);
        }
        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 -> 103;
                    case 1 -> 103;
                    case 2 -> 80;
                    case 3 -> 109;
                    case 4 -> 47;
                    case 5 -> 123;
                    default -> 112;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

