Beispiel #1
0
        public void Remove(Guid accountCode, Guid currentAccount)
        {
            var registeredAccount = accountService.GetIfHasPermissionToUpdate(currentAccount, accountCode, false, true);

            if (registeredAccount.IsNull())
            {
                throw new ArgumentException("Você não possui permissão para executar essa ação.");
            }

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    if (registeredAccount.AccountRoles.Count > 0)
                    {
                        foreach (var accRole in registeredAccount.AccountRoles.ToList())
                        {
                            accountRoleRepository.Delete(accRole.Code);
                        }
                    }

                    registeredAccount.SetRemoved();

                    accountRepository.Update(registeredAccount);
                }
                catch
                {
                    transaction.Rollback();

                    throw;
                }
            }
        }
Beispiel #2
0
        public Role Save(Role role)
        {
            var roleOld = roleRepository.Get(role.Code, role.StoreCode);

            if (!roleOld.IsNull() && !role.Permissions.IsNull())
            {
                permissionRepository.Delete(roleOld.Permissions);
            }

            if (!roleOld.IsNull() && !role.AccountRoles.IsNull())
            {
                accountRoleRepository.Delete(roleOld.AccountRoles);
            }

            if (!roleOld.IsNull() && !role.Restrictions.IsNull())
            {
                restrictionRepository.Delete(roleOld.Restrictions);
            }

            if (!role.Permissions.IsNull())
            {
                role.Permissions.ForEach(p =>
                {
                    p.RoleCode   = role.Code;
                    p.SaveDate   = DateTime.Now;
                    p.UpdateDate = DateTime.Now;

                    permissionRepository.Save(p);
                });
            }

            if (!role.AccountRoles.IsNull())
            {
                role.AccountRoles.ForEach(a =>
                {
                    a.RoleCode   = role.Code;
                    a.SaveDate   = DateTime.Now;
                    a.UpdateDate = DateTime.Now;

                    accountRoleRepository.Save(a);
                });
            }

            if (!role.Restrictions.IsNull())
            {
                role.Restrictions.ForEach(a =>
                {
                    a.RoleCode   = role.Code;
                    a.SaveDate   = DateTime.Now;
                    a.UpdateDate = DateTime.Now;

                    restrictionRepository.Save(a);
                });
            }

            return(roleRepository.Save(role));
        }
Beispiel #3
0
        public async Task <IResultModel> BindRole(AccountRoleBindModel model)
        {
            var account = await Get(model.AccountId);

            if (account == null || account.Deleted)
            {
                return(ResultModel.Failed("账户不存在"));
            }

            var exists = await _roleRepository.ExistsAsync(model.RoleId);

            if (!exists)
            {
                return(ResultModel.Failed("角色不存在"));
            }

            //添加
            if (model.Checked)
            {
                exists = await _accountRoleRepository.Exists(model.AccountId, model.RoleId);

                if (!exists)
                {
                    var result = await _accountRoleRepository.AddAsync(new AccountRoleEntity { AccountId = model.AccountId, RoleId = model.RoleId });
                    await ClearCache(result, account.Id);

                    return(ResultModel.Result(result));
                }
                await ClearCache(true, account.Id);

                return(ResultModel.Success());
            }
            {
                //删除
                var result = await _accountRoleRepository.Delete(model.AccountId, model.RoleId);
                await ClearCache(result, account.Id);

                return(ResultModel.Result(result));
            }
        }
        public async Task <IResultModel> BindRole(AccountRoleBindModel model)
        {
            var exists = await _accountRepository.ExistsAsync(model.AccountId);

            if (!exists)
            {
                return(ResultModel.Failed("账户不存在"));
            }

            exists = await _roleRepository.ExistsAsync(model.RoleId);

            if (!exists)
            {
                return(ResultModel.Failed("角色不存在"));
            }

            //添加
            if (model.Checked)
            {
                exists = await _accountRoleRepository.Exists(model.AccountId, model.RoleId);

                if (!exists)
                {
                    var result = await _accountRoleRepository.AddAsync(new AccountRole { AccountId = model.AccountId, RoleId = model.RoleId });

                    return(ResultModel.Result(result));
                }

                return(ResultModel.Success());
            }
            {
                //删除
                var result = await _accountRoleRepository.Delete(model.AccountId, model.RoleId);

                return(ResultModel.Result(result));
            }
        }