public async Task <ActionResult <BaseResponse> > Add(GroupAddViewModel req) { //超级管理员有权限 var GroupId = User.Claims.FirstOrDefault(a => a.Type == "GroupId").Value; var isAdmin = User.Claims.FirstOrDefault(a => a.Type == "IsAdmin").Value.ToLower() == "true" ? true : false; string Code = User.Claims.FirstOrDefault(a => a.Type == "Code").Value; string Account = User.Claims.FirstOrDefault(a => a.Type == "Account").Value; if (!(isAdmin && Code == _config["Group"])) { return(Unauthorized("用户没有权限添加组织")); } bool bGroup = await _gs.IsExist(a => a.GroupName == req.Name /*|| a.GroupCode == req.Code*/); if (bGroup) { return(new BaseResponse { Success = false, Message = "已存在相同名称的组织" }); } bGroup = await _gs.IsExist(a => a.GroupCode == req.Code); if (bGroup) { return(new BaseResponse { Success = false, Message = "已存在相同的组织代码" }); } bool bUser = await _us.IsExist(a => a.Account == req.Account); if (bUser) { return(new BaseResponse { Success = false, Message = "已存在相同的用户名" }); } var rm = await _gs.AddGroupAsync(req, Account); return(rm); }
public async Task <BaseResponse> AddGroupAsync(GroupAddViewModel req, string account) { //创建一个租户,连带创建该组织的一个账号,一个管理员角色 BaseResponse rm = new BaseResponse(); try { var gm = _mapper.Map <GroupModel>(req); gm.Id = EncryptData.CreateUUID(); gm.Create = account; string salt = EncryptData.CreateRandom(); string password = EncryptData.EncryptPassword(req.Password, salt); UserModel um = new UserModel() { Account = req.Account, Password = password, Salt = salt, CreateTime = DateTime.Now, GroupId = gm.Id, //IsAdmin = true, Status = Model.UserStatus.Valid, Create = account }; await _group.Add(gm, um); rm.Success = true; rm.Message = "添加组织成功"; _log.LogInformation($"{account}添加组织{gm.GroupName}成功"); } catch (Exception ex) { rm.Success = false; rm.Message = "添加组织失败,请联系管理员";// + ex.Message; _log.LogError($"{account}添加组织{req.Name}失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}"); } return(rm); }