//-----------------DepartAdmin---------------------- /// <summary> /// 给PersonId用户赋予roleId角色 /// </summary> /// <param name="personId">人员ID</param> /// <param name="roleId">角色ID</param> /// <param name="privilege">权限验证</param> /// <returns></returns> public static bool GrantRole(int personId, int roleId, Func <Person, bool> privilege) { using (var ctx = new AspodesDB()) { //用户状态是否正常 if (!ctx.Persons.Where(p => p.Status == "C").Any(p => p.PersonId == personId)) { throw new OtherException("用户状态错误"); } var user = ctx.Users.FirstOrDefault(u => u.PersonId == personId); //用户是否已经拥有该角色 var role = ctx.Authorizes.FirstOrDefault(a => a.UserId == user.UserId && a.RoleId == roleId); if (ctx.Authorizes.Any(a => a.UserId == user.UserId && a.RoleId == roleId)) { throw new OtherException("用户已经拥有该角色"); } //操作人是否有权限 if (!privilege(user.Person)) { throw new UnauthorizationException(); } Model.Authorize instAdmin = new Model.Authorize { RoleId = roleId, UserId = user.UserId }; ctx.Authorizes.Add(instAdmin); ctx.SaveChanges(); } return(true); }
private Institute AddInst(AspodesDB ctx, AddInstDTO dto) { var inst = Mapper.Map <Institute>(dto); var person = Mapper.Map <Person>(dto); //添加单位 var newInst = ctx.Institutes.Add(inst); ctx.SaveChanges(); //添加联系人 person.InstituteId = newInst.InstituteId; var newPerson = ctx.Persons.Add(person); ctx.SaveChanges(); //给联系人添加账户 User user = ctx.Users.Add(Mapper.Map <User>(newPerson)); ctx.SaveChanges(); //设置单位联系人 newInst.ContactId = user.UserId; //给联系人添加用户角色 Model.Authorize commen = new Model.Authorize { UserId = user.UserId, RoleId = 1 }; ctx.Authorizes.Add(commen); //给联系人添加单位管理员角色 Model.Authorize instAdmin = new Model.Authorize { UserId = user.UserId, RoleId = 2 }; ctx.Authorizes.Add(instAdmin); ctx.SaveChanges(); return(newInst); }
private Person AddPerson(AspodesDB ctx, Person person) { ctx.Persons.Add(person); ctx.SaveChanges(); Person newPerson = ctx.Persons.Where(c => c.IDCard == person.IDCard && c.Email == person.Email).First(); Institute personInst = ctx.Institutes.Where(c => c.InstituteId == newPerson.InstituteId).First(); if (personInst.Type != InstituteType.PARTNER) //添加外单位人员,不需要建立用户 { //添加用户 User user = Mapper.Map <User>(newPerson); User newUser = ctx.Users.Add(user); //添加用户角色 Model.Authorize authorize = new Model.Authorize() { UserId = newUser.UserId, RoleId = 1 }; ctx.Authorizes.Add(authorize); ctx.SaveChanges(); } return(newPerson); }
/// <summary> /// 添加院管理员 /// </summary> /// <param name="personDTO"></param> /// <returns></returns> public GetPersonDTO AddDepartAdmin(AddPersonDTO personDTO) { //人员属于农科院 personDTO.InstituteId = 1; using (var ctx = new AspodesDB()) { //是否检查人员信息重复添加 var old = ctx.Persons.FirstOrDefault(p => p.IDCard == personDTO.IDCard); if (null != old) { ResponseWrapper.ExceptionResponse(new OtherException("人员已经存在")); } using (var transaction = ctx.Database.BeginTransaction()) { try { //添加人员 Person person = Mapper.Map <Person>(personDTO); person.Name = person.FirstName + person.LastName; person.Status = "C"; Person newPerson = ctx.Persons.Add(person); ctx.SaveChanges(); //添加用户 User user = new User { UserId = newPerson.Email, IDCard = newPerson.IDCard, Name = newPerson.Name, PersonId = newPerson.PersonId, //Password = HashHelper.IntoMd5(newPerson.IDCard.Substring(newPerson.IDCard.Length - 6, 6)), Password = HashHelper.IntoMd5("123456"), InstituteId = newPerson.InstituteId }; User newUser = ctx.Users.Add(user); //添加用户角色 Model.Authorize authorize = new Model.Authorize() { UserId = newUser.UserId, RoleId = 1 }; ctx.Authorizes.Add(authorize); Model.Authorize departAdmin = new Model.Authorize() { UserId = newUser.UserId, RoleId = 3 }; ctx.Authorizes.Add(departAdmin); var instAdmins = from au in ctx.Authorizes where au.RoleId == 2 && au.User.InstituteId == 1 select au.UserId; if (instAdmins.Count() == 0) { Model.Authorize instAdmin = new Model.Authorize() { UserId = newUser.UserId, RoleId = 2 }; ctx.Authorizes.Add(instAdmin); var inst = ctx.Institutes.FirstOrDefault(i => i.InstituteId == 1); inst.ContactId = newUser.UserId; } ctx.SaveChanges(); transaction.Commit(); return(Mapper.Map <GetPersonDTO>(newPerson)); } catch (Exception e) { transaction.Rollback(); throw e; } } } }