void ExpandPermissions(List <uint> permissions)
        {
            List <uint> toCheck = new List <uint>(permissions);

            permissions.Clear();

            while (!toCheck.Empty())
            {
                // remove the permission from original list
                uint permissionId = toCheck.FirstOrDefault();
                toCheck.RemoveAt(0);

                RBACPermission permission = Global.AccountMgr.GetRBACPermission(permissionId);
                if (permission == null)
                {
                    continue;
                }

                // insert into the final list (expanded list)
                permissions.Add(permissionId);

                // add all linked permissions (that are not already expanded) to the list of permissions to be checked
                List <uint> linkedPerms = permission.GetLinkedPermissions();
                foreach (var id in linkedPerms)
                {
                    if (!permissions.Contains(id))
                    {
                        toCheck.Add(id);
                    }
                }
            }

            //Log.outDebug(LogFilter.General, "RBACData:ExpandPermissions: Expanded: {0}", GetDebugPermissionString(permissions));
        }
        public RBACCommandResult DenyPermission(uint permissionId, int realmId = 0)
        {
            // Check if permission Id exists
            RBACPermission perm = Global.AccountMgr.GetRBACPermission(permissionId);

            if (perm == null)
            {
                Log.outDebug(LogFilter.Rbac, "RBACData.DenyPermission [Id: {0} Name: {1}] (Permission {2}, RealmId {3}). Permission does not exists",
                             GetId(), GetName(), permissionId, realmId);
                return(RBACCommandResult.IdDoesNotExists);
            }

            // Check if already added in granted list
            if (HasGrantedPermission(permissionId))
            {
                Log.outDebug(LogFilter.Rbac, "RBACData.DenyPermission [Id: {0} Name: {1}] (Permission {2}, RealmId {3}). Permission in grant list",
                             GetId(), GetName(), permissionId, realmId);
                return(RBACCommandResult.InGrantedList);
            }

            // Already added?
            if (HasDeniedPermission(permissionId))
            {
                Log.outDebug(LogFilter.Rbac, "RBACData.DenyPermission [Id: {0} Name: {1}] (Permission {2}, RealmId {3}). Permission already denied",
                             GetId(), GetName(), permissionId, realmId);
                return(RBACCommandResult.CantAddAlreadyAdded);
            }

            AddDeniedPermission(permissionId);

            // Do not save to db when loading data from DB (realmId = 0)
            if (realmId != 0)
            {
                Log.outDebug(LogFilter.Rbac, "RBACData.DenyPermission [Id: {0} Name: {1}] (Permission {2}, RealmId {3}). Ok and DB updated",
                             GetId(), GetName(), permissionId, realmId);
                SavePermission(permissionId, false, realmId);
                CalculateNewPermissions();
            }
            else
            {
                Log.outDebug(LogFilter.Rbac, "RBACData.DenyPermission [Id: {0} Name: {1}] (Permission {2}, RealmId {3}). Ok",
                             GetId(), GetName(), permissionId, realmId);
            }

            return(RBACCommandResult.OK);
        }