Esempio n. 1
0
        public async Task <AjaxResult> EditUserAsync(User user)
        {
            string userid    = _accessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            string uasername = _accessor.HttpContext.User.Identity.Name;

            if (string.IsNullOrWhiteSpace(user.Id))
            {
                user.Id = Guid.NewGuid().ToString("N").ToUpper();
                user.Create(userid, uasername);
                user.LoginPWD = SecurityUtils.EncryptMD5("123456");

                UserRole[] userRoles = user.Roles.Select(t => new UserRole(user.Id, t)
                {
                    CreatedBy = uasername,
                    CreatedId = uasername
                }).ToArray();

                var db = _dal.GetDatabase();
                await db.UseDbTransactionAsync(async (conn, tran) =>
                {
                    await _dal.DeleteAsync <UserRole>("UserId", user.Id, tran);
                    await _dal.AddAsync(userRoles, tran);
                    await _dal.AddAsync(user, tran);
                });

                return(Success("添加成功"));
            }
            else
            {
                user.Update(userid, uasername);
                user.LastLoginTime = DateTime.Now;

                UserRole[] userRoles = user.Roles.Select(t => new UserRole(user.Id, t)
                {
                    CreatedBy = uasername,
                    CreatedId = uasername,
                }).ToArray();

                var db = _dal.GetDatabase();
                await db.UseDbTransactionAsync(async (conn, tran) =>
                {
                    await _dal.DeleteAsync <UserRole>("UserId", user.Id, tran);
                    await _dal.AddAsync(userRoles, tran);
                    await _dal.EditAsync(user, new string[] { "LoginPWD" }, tran);
                });

                return(Success("修改成功"));
            }
        }