Exemplo n.º 1
0
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="sysUserId"></param>
        /// <param name="sysRoleId"></param>
        /// <returns></returns>
        public async Task EditSysUserRole(Guid sysUserId, Guid[] sysRoleId)
        {
            using (var sysUserRoleDao = new SysUserRoleDao())
            {
                using (var sysUserDao = new SysUserDao())
                {
                    var user = await sysUserDao.GetOneByIdAsync(sysUserId);

                    foreach (var userRole in user.SysUserRoles)
                    {
                        if (sysRoleId.Contains(userRole.SysRoleId))
                        {
                            await sysUserRoleDao.EditAsync(userRole, false);
                        }
                    }
                    await sysUserRoleDao.Save();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sysUser"></param>
        /// <returns></returns>
        public async Task EditSysUser(SysUser sysUser, Guid[] sysRoleIds)
        {
            using (var sysUserDao = new SysUserDao())
            {
                var m = await sysUserDao.GetOneByIdAsync(sysUser.Id);

                m.UserName    = sysUser.UserName;
                m.RealName    = sysUser.RealName;
                m.SysDepartId = sysUser.SysDepartId;
                await sysUserDao.EditAsync(m);

                //中间表处理
                if (sysRoleIds != null)
                {
                    using (var sysUserRoleDao = new SysUserRoleDao())
                    {
                        var sysUserRoleList = sysUserRoleDao.GetAllAsync().Where(a => a.SysUserId == sysUser.Id);
                        //先清空
                        foreach (var item in sysUserRoleList)
                        {
                            await sysUserRoleDao.RemoveAsync(item, false);
                        }
                        await sysUserRoleDao.Save();

                        //添加
                        foreach (var roleId in sysRoleIds)
                        {
                            await sysUserRoleDao.CreateAsync(new SysUserRole()
                            {
                                SysUserId = sysUser.Id,
                                SysRoleId = roleId
                            }, false);
                        }
                        await sysUserRoleDao.Save();
                    }
                }
            }
        }