/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.security.rbac.Role
 *  com.l7tech.gateway.common.security.rbac.Role$Tag
 *  com.l7tech.gateway.common.security.rbac.RoleAssignment
 *  com.l7tech.identity.Group
 *  com.l7tech.identity.IdentityProvider
 *  com.l7tech.identity.User
 *  com.l7tech.identity.internal.InternalGroup
 *  com.l7tech.identity.internal.InternalUser
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.IdentityHeader
 *  com.l7tech.objectmodel.UpdateException
 *  org.jetbrains.annotations.NotNull
 */
package com.l7tech.server.security.rbac;

import com.l7tech.gateway.common.security.rbac.Role;
import com.l7tech.gateway.common.security.rbac.RoleAssignment;
import com.l7tech.identity.Group;
import com.l7tech.identity.IdentityProvider;
import com.l7tech.identity.User;
import com.l7tech.identity.internal.InternalGroup;
import com.l7tech.identity.internal.InternalUser;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.IdentityHeader;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.server.identity.internal.InternalGroupManager;
import com.l7tech.server.identity.internal.InternalIdentityProvider;
import com.l7tech.server.identity.internal.InternalUserManager;
import com.l7tech.server.security.rbac.RoleManager;
import com.l7tech.server.security.rbac.RoleManagerIdentitySource;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.jetbrains.annotations.NotNull;

public abstract class RoleManagerIdentitySourceSupport
implements RoleManagerIdentitySource {
    protected RoleManager roleManager;
    private static final String[] v;

    /*
     * Loose catch block
     */
    @Override
    public void validateRoleAssignments() throws UpdateException {
        block28: {
            try {
                boolean bl2;
                block32: {
                    InternalUser internalUser;
                    IdentityProvider identityProvider;
                    Set<IdentityProvider> set = this.getAdminIdentityProviders();
                    bl2 = false;
                    try {
                        IdentityProvider identityProvider2 = identityProvider = !set.isEmpty() ? set.iterator().next() : null;
                    }
                    catch (FindException findException) {
                        throw RoleManagerIdentitySourceSupport.a(findException);
                    }
                    if (!(identityProvider instanceof InternalIdentityProvider)) break block28;
                    InternalIdentityProvider internalIdentityProvider = (InternalIdentityProvider)identityProvider;
                    InternalUserManager internalUserManager = (InternalUserManager)internalIdentityProvider.getUserManager();
                    InternalGroupManager internalGroupManager = (InternalGroupManager)internalIdentityProvider.getGroupManager();
                    Role role = this.roleManager.findByTag(Role.Tag.ADMIN);
                    if (role == null) break block28;
                    HashSet<String> hashSet = new HashSet<String>();
                    for (RoleAssignment roleAssignment : role.getRoleAssignments()) {
                        block29: {
                            try {
                                if (!roleAssignment.getProviderId().equals((Object)internalIdentityProvider.getConfig().getGoid()) || !EntityType.USER.getName().equals(roleAssignment.getEntityType())) continue;
                            }
                            catch (FindException findException) {
                                throw RoleManagerIdentitySourceSupport.a(findException);
                            }
                            internalUser = (InternalUser)internalUserManager.findByPrimaryKey(roleAssignment.getIdentityId());
                            if (internalUser == null) break block29;
                            try {
                                block31: {
                                    if (!internalUser.isEnabled()) break block29;
                                    break block31;
                                    catch (FindException findException) {
                                        throw RoleManagerIdentitySourceSupport.a(findException);
                                    }
                                }
                                if (internalUser.getExpiration() >= 0L) break block29;
                            }
                            catch (FindException findException) {
                                throw RoleManagerIdentitySourceSupport.a(findException);
                            }
                            bl2 = true;
                            break;
                        }
                        hashSet.add(roleAssignment.getIdentityId());
                    }
                    if (bl2) break block32;
                    block23: for (RoleAssignment roleAssignment : role.getRoleAssignments()) {
                        try {
                            if (!roleAssignment.getProviderId().equals((Object)internalIdentityProvider.getConfig().getGoid()) || !EntityType.GROUP.getName().equals(roleAssignment.getEntityType())) continue;
                        }
                        catch (FindException findException) {
                            throw RoleManagerIdentitySourceSupport.a(findException);
                        }
                        internalUser = (InternalGroup)internalGroupManager.findByPrimaryKey(roleAssignment.getIdentityId());
                        try {
                            if (!internalUser.isEnabled()) {
                                continue;
                            }
                        }
                        catch (FindException findException) {
                            throw RoleManagerIdentitySourceSupport.a(findException);
                        }
                        Set set2 = internalGroupManager.getUserHeaders(roleAssignment.getIdentityId());
                        for (IdentityHeader identityHeader : set2) {
                            block30: {
                                try {
                                    if (hashSet.contains(identityHeader.getStrId())) {
                                        continue;
                                    }
                                }
                                catch (FindException findException) {
                                    throw RoleManagerIdentitySourceSupport.a(findException);
                                }
                                InternalUser internalUser2 = (InternalUser)internalUserManager.findByPrimaryKey(identityHeader.getStrId());
                                if (internalUser2 == null) break block30;
                                try {
                                    block33: {
                                        if (!internalUser2.isEnabled()) break block30;
                                        break block33;
                                        catch (FindException findException) {
                                            throw RoleManagerIdentitySourceSupport.a(findException);
                                        }
                                    }
                                    if (internalUser2.getExpiration() >= 0L) break block30;
                                }
                                catch (FindException findException) {
                                    throw RoleManagerIdentitySourceSupport.a(findException);
                                }
                                bl2 = true;
                                continue block23;
                            }
                            hashSet.add(roleAssignment.getIdentityId());
                        }
                    }
                }
                try {
                    if (!bl2) {
                        throw new UpdateException(v[2]);
                    }
                }
                catch (FindException findException) {
                    throw RoleManagerIdentitySourceSupport.a(findException);
                }
            }
            catch (FindException findException) {
                throw new UpdateException(v[0], (Throwable)findException);
            }
        }
    }

    @Override
    public Set<IdentityHeader> getGroups(User user, boolean bl2) throws FindException {
        return Collections.emptySet();
    }

    @Override
    public Set<IdentityHeader> getGroups(@NotNull Group group) throws FindException {
        try {
            if (group == null) {
                RoleManagerIdentitySourceSupport.a(0);
            }
        }
        catch (FindException findException) {
            throw RoleManagerIdentitySourceSupport.a(findException);
        }
        return Collections.emptySet();
    }

    protected abstract Set<IdentityProvider> getAdminIdentityProviders();

    private static /* synthetic */ void a(int n2) {
        String[] stringArray = v;
        throw new IllegalArgumentException(String.format(stringArray[3], stringArray[1], stringArray[4], stringArray[5]));
    }

    private static FindException a(FindException findException) {
        return findException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "8Ul!6Y&\u0015B}%-\u0017\"]Uq\"!Y$\u000eTw)*\u001c(\u0018Ij=j\u0005\u001aUq;4x<S>\"!\u00186\t\u0007q !Y,\u0013S{<*\u0018)]Rm+6Y2\u0014Svn*\u0016e\u0018_n'6\u00181\u0014Hpn)\f6\t\u0007|+d\u00186\u000eNy !\u001de\tH>:,\u001ce\u001cCs'*\u00106\tU\u007f:-\u000f ]Uq\"!Ym\u001eFpn&\u001ce\u000bN\u007fn#\u000b*\bW>/7\n,\u001aIs+*\rlS><Uy;)\u001c+\t\u0007x!6Y\u00053Hj\u00001\u0015)]W\u007f<%\u0014 \tBlnc\\6Z\u0007q(d\\6S\u0002mn)\f6\t\u0007p!0Y'\u0018\u0007p;(\u0015";
                var4_3 = "8Ul!6Y&\u0015B}%-\u0017\"]Uq\"!Y$\u000eTw)*\u001c(\u0018Ij=j\u0005\u001aUq;4x<S>\"!\u00186\t\u0007q !Y,\u0013S{<*\u0018)]Rm+6Y2\u0014Svn*\u0016e\u0018_n'6\u00181\u0014Hpn)\f6\t\u0007|+d\u00186\u000eNy !\u001de\tH>:,\u001ce\u001cCs'*\u00106\tU\u007f:-\u000f ]Uq\"!Ym\u001eFpn&\u001ce\u000bN\u007fn#\u000b*\bW>/7\n,\u001aIs+*\rlS><Uy;)\u001c+\t\u0007x!6Y\u00053Hj\u00001\u0015)]W\u007f<%\u0014 \tBlnc\\6Z\u0007q(d\\6S\u0002mn)\f6\t\u0007p!0Y'\u0018\u0007p;(\u0015".length();
                var1_4 = 33;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = RoleManagerIdentitySourceSupport.a(v0, RoleManagerIdentitySourceSupport.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "k=\u0006\u0014];Dm1\u0003\u0014BiB~7\u0019\u0014BiS} \u0002OH#Bj3\b\u0014cc\\m\u001f\nUPkUz\u001b\u000f^_xY|+8TD~Sm\u0001\u001eKAcB|\to7\u001f|CcEx!";
                    var4_3 = "k=\u0006\u0014];Dm1\u0003\u0014BiB~7\u0019\u0014BiS} \u0002OH#Bj3\b\u0014cc\\m\u001f\nUPkUz\u001b\u000f^_xY|+8TD~Sm\u0001\u001eKAcB|\to7\u001f|CcEx!".length();
                    var1_4 = 64;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 77;
                        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++] = RoleManagerIdentitySourceSupport.a(v0, RoleManagerIdentitySourceSupport.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;
            }
        }
        RoleManagerIdentitySourceSupport.v = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x7D);
        }
        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 -> 69;
                    case 1 -> 31;
                    case 2 -> 38;
                    case 3 -> 118;
                    case 4 -> 124;
                    case 5 -> 65;
                    default -> 125;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

