/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.security.rbac.OperationType
 *  com.l7tech.gateway.common.security.rbac.Role
 *  com.l7tech.identity.IdentityProviderConfig
 *  com.l7tech.identity.IdentityProviderConfigManager
 *  com.l7tech.identity.IdentityProviderType
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.Entity
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.SaveException
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.objectmodel.imp.NamedEntityImp
 *  com.l7tech.util.Config
 *  com.l7tech.util.ConfigFactory
 *  org.jetbrains.annotations.NotNull
 *  org.springframework.dao.DataAccessException
 */
package com.l7tech.server.identity;

import com.l7tech.gateway.common.security.rbac.OperationType;
import com.l7tech.gateway.common.security.rbac.Role;
import com.l7tech.identity.IdentityProviderConfig;
import com.l7tech.identity.IdentityProviderConfigManager;
import com.l7tech.identity.IdentityProviderType;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.Entity;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.objectmodel.imp.NamedEntityImp;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.security.rbac.RoleManager;
import com.l7tech.server.util.JaasUtils;
import com.l7tech.util.Config;
import com.l7tech.util.ConfigFactory;
import java.text.MessageFormat;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.springframework.dao.DataAccessException;

public class IdProvConfManagerServer
extends HibernateEntityManager<IdentityProviderConfig, EntityHeader>
implements IdentityProviderConfigManager {
    private static final Logger m;
    private RoleManager n;
    private Config o;
    private static final Pattern p;
    static final String q;
    private static final String[] r;

    public void setRoleManager(RoleManager roleManager) {
        this.n = roleManager;
    }

    public void setConfig(@NotNull Config config) {
        try {
            if (config == null) {
                IdProvConfManagerServer.b(0);
            }
        }
        catch (DataAccessException dataAccessException) {
            throw IdProvConfManagerServer.a((Exception)((Object)dataAccessException));
        }
        this.o = config;
    }

    @Override
    public Goid save(IdentityProviderConfig identityProviderConfig) throws SaveException {
        try {
            if (identityProviderConfig.type() == IdentityProviderType.INTERNAL) {
                this.a(identityProviderConfig);
            }
        }
        catch (SaveException saveException) {
            throw IdProvConfManagerServer.a((Exception)((Object)saveException));
        }
        return super.save(identityProviderConfig);
    }

    @Override
    public void update(IdentityProviderConfig identityProviderConfig) throws UpdateException {
        block7: {
            block6: {
                try {
                    if (identityProviderConfig.type() != IdentityProviderType.INTERNAL) break block6;
                    this.a(identityProviderConfig);
                    break block7;
                }
                catch (FindException findException) {
                    throw IdProvConfManagerServer.a((Exception)((Object)findException));
                }
            }
            try {
                this.n.renameEntitySpecificRoles(EntityType.ID_PROVIDER_CONFIG, (NamedEntityImp)identityProviderConfig, p);
            }
            catch (FindException findException) {
                throw new UpdateException(r[0], (Throwable)findException);
            }
        }
        try {
            super.update(identityProviderConfig);
        }
        catch (DataAccessException dataAccessException) {
            throw new UpdateException(dataAccessException.toString(), (Throwable)dataAccessException);
        }
    }

    @Override
    public void delete(Goid goid) throws DeleteException, FindException {
        this.findAndDelete(goid);
    }

    @Override
    public void delete(IdentityProviderConfig identityProviderConfig) throws DeleteException {
        try {
            if (identityProviderConfig.type() == IdentityProviderType.INTERNAL) {
                m.warning(r[7]);
                throw new DeleteException(r[9]);
            }
        }
        catch (DataAccessException dataAccessException) {
            throw IdProvConfManagerServer.a((Exception)((Object)dataAccessException));
        }
        try {
            super.delete(identityProviderConfig);
        }
        catch (DataAccessException dataAccessException) {
            throw new DeleteException(dataAccessException.toString(), (Throwable)dataAccessException);
        }
    }

    public Class<IdentityProviderConfig> getImpClass() {
        return IdentityProviderConfig.class;
    }

    @Override
    public Class<IdentityProviderConfig> getInterfaceClass() {
        return IdentityProviderConfig.class;
    }

    @Override
    public EntityType getEntityType() {
        return EntityType.ID_PROVIDER_CONFIG;
    }

    public void createRoles(IdentityProviderConfig identityProviderConfig) throws SaveException {
        try {
            if (this.o.getBooleanProperty(r[3], true)) {
                this.b(identityProviderConfig);
            }
        }
        catch (SaveException saveException) {
            throw IdProvConfManagerServer.a((Exception)((Object)saveException));
        }
    }

    public void updateRoles(IdentityProviderConfig identityProviderConfig) throws UpdateException {
    }

    protected EntityHeader headerFromConfig(IdentityProviderConfig identityProviderConfig) {
        EntityHeader entityHeader = new EntityHeader();
        entityHeader.setDescription(identityProviderConfig.getDescription());
        entityHeader.setName(identityProviderConfig.getName());
        entityHeader.setGoid(identityProviderConfig.getGoid());
        entityHeader.setType(EntityType.ID_PROVIDER_CONFIG);
        return entityHeader;
    }

    private void a(IdentityProviderConfig identityProviderConfig) {
        String[] stringArray = r;
        identityProviderConfig.setName(stringArray[10]);
        identityProviderConfig.setDescription(stringArray[19]);
    }

    private void b(IdentityProviderConfig identityProviderConfig) throws SaveException {
        Role role;
        block13: {
            boolean bl2;
            User user = JaasUtils.getCurrentUser();
            String[] stringArray = r;
            String string = MessageFormat.format(stringArray[8], identityProviderConfig.getName(), identityProviderConfig.getGoid());
            m.info(stringArray[15] + string);
            role = new Role();
            try {
                role.setName(string);
                role.addEntityPermission(OperationType.READ, EntityType.ID_PROVIDER_CONFIG, identityProviderConfig.getId());
                role.addEntityPermission(OperationType.UPDATE, EntityType.ID_PROVIDER_CONFIG, identityProviderConfig.getId());
                role.addEntityPermission(OperationType.DELETE, EntityType.ID_PROVIDER_CONFIG, identityProviderConfig.getId());
                role.addAttributePermission(OperationType.CREATE, EntityType.USER, stringArray[16], identityProviderConfig.getId());
                role.addAttributePermission(OperationType.READ, EntityType.USER, stringArray[13], identityProviderConfig.getId());
                role.addAttributePermission(OperationType.UPDATE, EntityType.USER, stringArray[13], identityProviderConfig.getId());
                role.addAttributePermission(OperationType.DELETE, EntityType.USER, stringArray[13], identityProviderConfig.getId());
                role.addAttributePermission(OperationType.CREATE, EntityType.GROUP, stringArray[13], identityProviderConfig.getId());
                role.addAttributePermission(OperationType.READ, EntityType.GROUP, stringArray[13], identityProviderConfig.getId());
                role.addAttributePermission(OperationType.UPDATE, EntityType.GROUP, stringArray[13], identityProviderConfig.getId());
                role.addAttributePermission(OperationType.DELETE, EntityType.GROUP, stringArray[13], identityProviderConfig.getId());
                role.addEntityPermission(OperationType.READ, EntityType.SSG_KEY_ENTRY, null);
                role.addEntityPermission(OperationType.READ, EntityType.SSG_KEYSTORE, null);
                role.setEntityType(EntityType.ID_PROVIDER_CONFIG);
                role.setEntityGoid(identityProviderConfig.getGoid());
                role.setDescription(stringArray[12]);
                bl2 = identityProviderConfig.type() == IdentityProviderType.FEDERATED;
            }
            catch (FindException findException) {
                throw IdProvConfManagerServer.a((Exception)((Object)findException));
            }
            boolean bl3 = bl2;
            try {
                if (bl3) {
                    role.addEntityPermission(OperationType.READ, EntityType.TRUSTED_CERT, null);
                }
            }
            catch (FindException findException) {
                throw IdProvConfManagerServer.a((Exception)((Object)findException));
            }
            if (user != null) {
                boolean bl4;
                try {
                    bl4 = this.n.isPermittedForEntity(user, (Entity)identityProviderConfig, OperationType.READ, null);
                    bl4 &= this.n.isPermittedForEntity(user, (Entity)identityProviderConfig, OperationType.UPDATE, null);
                    bl4 &= this.n.isPermittedForEntity(user, (Entity)identityProviderConfig, OperationType.DELETE, null);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.CREATE, EntityType.USER);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.READ, EntityType.USER);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.UPDATE, EntityType.USER);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.DELETE, EntityType.USER);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.CREATE, EntityType.GROUP);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.READ, EntityType.GROUP);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.UPDATE, EntityType.GROUP);
                    bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.DELETE, EntityType.GROUP);
                    if (bl3) {
                        bl4 &= this.n.isPermittedForAnyEntityOfType(user, OperationType.READ, EntityType.TRUSTED_CERT);
                    }
                }
                catch (FindException findException) {
                    stringArray = r;
                    throw new SaveException(stringArray[14], (Throwable)findException);
                }
                try {
                    try {
                        if (bl4 || !this.a()) break block13;
                    }
                    catch (FindException findException) {
                        throw IdProvConfManagerServer.a((Exception)((Object)findException));
                    }
                    m.info(r[5]);
                    role.addAssignedUser(user);
                }
                catch (FindException findException) {
                    throw IdProvConfManagerServer.a((Exception)((Object)findException));
                }
            }
        }
        this.n.save((PersistentEntity)role);
    }

    private boolean a() {
        return ConfigFactory.getBooleanProperty((String)r[1], (boolean)true);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[21];
                var3_1 = 0;
                var2_2 = "\u000fEz\u0001lV.8\ni\u0004f\\)\u001eEc\b(LflXj\u0003iUl'>Hn\u000e&Y|8E]\u0002d]'!Ka\fo]Y>Ey\u0004l]{bKz\u0019gyz?Ch\u0003>\rXh\u0018e]g8\ni\u0002z\u0018I\u0002E{#}TelZn\u001fiUl8O}M/\u001dzk\n`\u000b(\u001dzb\u000f|MeMz8\na\u0002|\u0018k)\na\u0018dT'>Hn\u000e&Y|8E]\u0002d]'!Ka\fo]Y>Ey\u0004l]{bKz\u0019g{{)K{\b\t?O{.gVo%M\"\rY|\u0004oV`\"M/\u000e}J{)D{M]Kl>\n{\u0002(Vl;\n]\u0002d]\u0011\u0005Nj\u0003|Q}5\n_\u001fgN`(O}&\r^{\beH}l^`Ml]e)^jMaV})Xa\fd\u0018`(\n\u007f\u001fgN`(O}#\u0001Ka\fo])7\u001arMA\\l\"^f\u0019q\u0018Y>Ey\u0004l]{l\u0002,\u00169E %8Bf\u001e(Lp<O/\u0002n\u0018j#Di\u0004o\u0018j-Da\u0002|\u0018k)\nk\bd]})N\u001a\u0005D{\bzVh \nF\tmV}%^vMXJf:Ck\bz2/EbBd\u000f})IgB{]{:O}Ba\\l\"^f\u0019q\u0017@(z}\u0002~{f\"LB\ffYn)X\\\bzNl>\u0098\u0019Yj\u001f{\u0018h?Yf\nf]ml^`M|PllQ?\u0010(Jf O/\u0005iNll^g\b(Yk%Ff\u0019q\u0018}#\n}\bi\\%l_\u007f\tiLllKa\t(\\l O{\b(La)\nt\\u\u0018y>Ey\u0004l]{`\nn\u0003l\u0018j>On\u0019m\u0014)?On\u001fkP%l_\u007f\tiLllKa\t(\\l O{\b(Q}?\nz\u001emJzlKa\t(_{#_\u007f\u001e&\n<X`\u001ba\\l>ck!\u000fEz\u0001lV.8\nh\b|\u0018l4C|\u0019aVnlZj\u001feQz?C`\u0003{\u0013\u000fXj\f|Qg+\na\b\u007f\u0018[#FjW(\n<X`\u001ba\\l>ck\u0006/Ea\u000ba_=d\u00153PVd~kQ(\\$\t;t\rrJ(\u0011'f\u0015'R5\u0018r|W/1 \u001bR\u0010NT1l\u0014)\u0011VT,%bhaP?@1eTfv&I!";
                var4_3 = "\u000fEz\u0001lV.8\ni\u0004f\\)\u001eEc\b(LflXj\u0003iUl'>Hn\u000e&Y|8E]\u0002d]'!Ka\fo]Y>Ey\u0004l]{bKz\u0019gyz?Ch\u0003>\rXh\u0018e]g8\ni\u0002z\u0018I\u0002E{#}TelZn\u001fiUl8O}M/\u001dzk\n`\u000b(\u001dzb\u000f|MeMz8\na\u0002|\u0018k)\na\u0018dT'>Hn\u000e&Y|8E]\u0002d]'!Ka\fo]Y>Ey\u0004l]{bKz\u0019g{{)K{\b\t?O{.gVo%M\"\rY|\u0004oV`\"M/\u000e}J{)D{M]Kl>\n{\u0002(Vl;\n]\u0002d]\u0011\u0005Nj\u0003|Q}5\n_\u001fgN`(O}&\r^{\beH}l^`Ml]e)^jMaV})Xa\fd\u0018`(\n\u007f\u001fgN`(O}#\u0001Ka\fo])7\u001arMA\\l\"^f\u0019q\u0018Y>Ey\u0004l]{l\u0002,\u00169E %8Bf\u001e(Lp<O/\u0002n\u0018j#Di\u0004o\u0018j-Da\u0002|\u0018k)\nk\bd]})N\u001a\u0005D{\bzVh \nF\tmV}%^vMXJf:Ck\bz2/EbBd\u000f})IgB{]{:O}Ba\\l\"^f\u0019q\u0017@(z}\u0002~{f\"LB\ffYn)X\\\bzNl>\u0098\u0019Yj\u001f{\u0018h?Yf\nf]ml^`M|PllQ?\u0010(Jf O/\u0005iNll^g\b(Yk%Ff\u0019q\u0018}#\n}\bi\\%l_\u007f\tiLllKa\t(\\l O{\b(La)\nt\\u\u0018y>Ey\u0004l]{`\nn\u0003l\u0018j>On\u0019m\u0014)?On\u001fkP%l_\u007f\tiLllKa\t(\\l O{\b(Q}?\nz\u001emJzlKa\t(_{#_\u007f\u001e&\n<X`\u001ba\\l>ck!\u000fEz\u0001lV.8\nh\b|\u0018l4C|\u0019aVnlZj\u001feQz?C`\u0003{\u0013\u000fXj\f|Qg+\na\b\u007f\u0018[#FjW(\n<X`\u001ba\\l>ck\u0006/Ea\u000ba_=d\u00153PVd~kQ(\\$\t;t\rrJ(\u0011'f\u0015'R5\u0018r|W/1 \u001bR\u0010NT1l\u0014)\u0011VT,%bhaP?@1eTfv&I!".length();
                var1_4 = 28;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = IdProvConfManagerServer.b(v0, IdProvConfManagerServer.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "-lS R~@\b\"n!E~U\rv^epbN\u0012kC R'\u0016`F&\u000eqT\u0010mu*Lu\u000f\tcI$Guq\u0016mQ,DuSJcR1OSS\u0001cS ";
                    var4_3 = "-lS R~@\b\"n!E~U\rv^epbN\u0012kC R'\u0016`F&\u000eqT\u0010mu*Lu\u000f\tcI$Guq\u0016mQ,DuSJcR1OSS\u0001cS ".length();
                    var1_4 = 26;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 95;
                        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++] = IdProvConfManagerServer.b(v0, IdProvConfManagerServer.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;
            }
        }
        IdProvConfManagerServer.r = var5;
        IdProvConfManagerServer.q = IdProvConfManagerServer.r[20];
        IdProvConfManagerServer.m = Logger.getLogger(IdProvConfManagerServer.class.getName());
        var0_6 = IdProvConfManagerServer.r;
        IdProvConfManagerServer.p = Pattern.compile(MessageFormat.format(var0_6[18], new Object[]{var0_6[6]}));
    }

    private static /* synthetic */ void b(int n2) {
        String[] stringArray = r;
        throw new IllegalArgumentException(String.format(stringArray[2], stringArray[17], stringArray[11], stringArray[4]));
    }

    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] ^ 0x7E);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(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 -> 59;
                    case 1 -> 93;
                    case 2 -> 120;
                    case 3 -> 26;
                    case 4 -> 127;
                    case 5 -> 79;
                    default -> 126;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

