<commit_before>import Vapor

class Gate {
    
    public let error = Abort.custom(status: .forbidden, message: "User does not have access to this page")
    
    /// Check if a backend_users.role is allowed to access
    ///
    /// - Parameters:
    ///   - backendUserRole: role from config
    ///   - role: role from config
    /// - Returns: bool
    public func allow(_ backendUserRole: String, _ role: String) -> Bool {
        guard let roles = Configuration.shared?.roles else {
            print("AdminPanel.Gate missing configuration")
            return false
        }
        
        // Find role
        var foundRoleOpt: Role?
        for roleObj in roles {
            
            if roleObj.slug.lowercased() == role.lowercased() {
                foundRoleOpt = roleObj
            }
        }
        
        // Unwrap
        guard let foundRole = foundRoleOpt else {
            print("AdminPanel.Gate role was not found")
            
            return false
        }
        
        // Not make a list of all roles allowed, which is all roles from found role and above in array
        var allowedRoles: [Role] = []
        for roleObj in roles {
            allowedRoles.append(roleObj)
            
            if roleObj.slug == foundRole.slug {
                break
            }
        }
        
        // Loop allowed routes and check if backendUser has one of them
        for roleObj in allowedRoles {
            if roleObj.slug == backendUserRole {
                return true
            }
        }
        
        
        return false
    }
    
    public func disallow(_ backendUserRole: String, _ role: String) -> Bool {
        return !allow(backendUserRole, role)
    }
    
    public func allowOrFail(_ backendUserRole: String, _ role: String) throws {
        if disallow(backendUserRole, role) {
            throw error
        }
    }
    
    public func allow(_ backendUser: BackendUser, _ role: String) -> Bool {
        return allow(backendUser.role, role)
    }
    
    public func disallow(_ backendUser: BackendUser, _ role: String) -> Bool {
        return !allow(backendUser.role, role)
    }
    
    public func allowOrFail(_ backendUser: BackendUser, _ role: String) throws {
        if disallow(backendUser, role) {
            throw error
        }
    }
}<commit_msg>updated gate system, to be more flexible for user request.auth.user<commit_after>import Vapor

class Gate {
    
    public static let error = Abort.custom(status: .forbidden, message: "User does not have access to this page")
    
    /// Check if a backend_users.role is allowed to access
    ///
    /// - Parameters:
    ///   - backendUserRole: role from config
    ///   - role: role from config
    /// - Returns: bool
    public static func allow(_ backendUserRole: String, _ role: String) -> Bool {
        guard let roles = Configuration.shared?.roles else {
            print("AdminPanel.Gate missing configuration")
            return false
        }
        
        // Find role
        var foundRoleOpt: Role?
        for roleObj in roles {
            
            if roleObj.slug.lowercased() == role.lowercased() {
                foundRoleOpt = roleObj
            }
        }
        
        // Unwrap
        guard let foundRole = foundRoleOpt else {
            print("AdminPanel.Gate role was not found")
            
            return false
        }
        
        // Not make a list of all roles allowed, which is all roles from found role and above in array
        var allowedRoles: [Role] = []
        for roleObj in roles {
            allowedRoles.append(roleObj)
            
            if roleObj.slug == foundRole.slug {
                break
            }
        }
        
        // Loop allowed routes and check if backendUser has one of them
        for roleObj in allowedRoles {
            if roleObj.slug == backendUserRole {
                return true
            }
        }
        
        
        return false
    }
    
    public static func disallow(_ backendUserRole: String, _ role: String) -> Bool {
        return !self.allow(backendUserRole, role)
    }
    
    public static func allowOrFail(_ backendUserRole: String, _ role: String) throws {
        if self.disallow(backendUserRole, role) {
            throw self.error
        }
    }
    
    public static func allow(_ backendUser: BackendUser?, _ role: String) -> Bool {
        return self.allow(backendUser?.role ?? "", role)
    }
    
    public static func disallow(_ backendUser: BackendUser?, _ role: String) -> Bool {
        return !self.allow(backendUser?.role ?? "", role)
    }
    
    public static func allowOrFail(_ backendUser: BackendUser?, _ role: String) throws {
        if self.disallow(backendUser, role) {
            throw self.error
        }
    }
}
{'fraction_non_alphanumeric': 0.0803552984920471, 'fraction_numerical': 0.0, 'mean_word_length': 1.603225806451613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '914231', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1256, 'n_words': 404}