<commit_before>//
//  UserPermissions.swift
//  Rocket.Chat
//
//  Created by <NAME> on 5/16/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import RealmSwift

extension User {
    func rolesInSubscription(_ subscription: Subscription) -> [String] {
        if let roles = subscription.usersRoles.first(where: { $0.user?.identifier == self.identifier })?.roles {
            return Array(roles)
        }

        return []
    }

    func hasPermission(_ permission: PermissionType, subscription: Subscription? = nil, realm: Realm? = Realm.current) -> Bool {
        guard let permissionRoles = PermissionManager.roles(for: permission, realm: realm) else { return false }

        let roles: [String]
        if let subscription = subscription {
            roles = rolesInSubscription(subscription)
        } else {
            roles = Array(self.roles)
        }

        for userRole in roles {
            for permissionRole in permissionRoles where userRole == permissionRole {
                return true
            }
        }
<commit_msg>Take server roles into account in room context<commit_after>//
//  UserPermissions.swift
//  Rocket.Chat
//
//  Created by <NAME> on 5/16/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//
import RealmSwift

extension User {
    func rolesInSubscription(_ subscription: Subscription) -> [String] {
        if let roles = subscription.usersRoles.first(where: { $0.user?.identifier == self.identifier })?.roles {
            return Array(roles)
        }

        return []
    }

    func hasPermission(_ permission: PermissionType, subscription: Subscription? = nil, realm: Realm? = Realm.current) -> Bool {
        guard let permissionRoles = PermissionManager.roles(for: permission, realm: realm) else { return false }

        var roles: [String] = Array(self.roles)
        if let subscription = subscription {
            roles += rolesInSubscription(subscription)
        }

        for userRole in roles {
            for permissionRole in permissionRoles where userRole == permissionRole {
                return true
            }
        }

{'fraction_non_alphanumeric': 0.09934086629001883, 'fraction_numerical': 0.009416195856873822, 'mean_word_length': 2.3464566929133857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1877989', 'n_tokens_mistral': 583, 'n_tokens_neox': 561, 'n_words': 165}