public async Task <IActionResult> UpdateProfile(string userName, [FromBody] User user) { if (!UserAthentic(userName)) { return(BadRequest("You don't have permissions for this action!")); } try { var nuser = await _context.UserList.SingleOrDefaultAsync(user => user.UserName == userName); nuser.FirstName = user.FirstName; nuser.LastName = user.LastName; nuser.NickName = user.NickName; nuser.Introduction = user.Introduction; nuser.Interests = user.Interests; nuser.School = user.School; nuser.City = user.City; await _context.SaveChangesAsync(); return(Ok()); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(NotFound()); } }
public async Task <IActionResult> UploadFile() { var formFile = Request.Form.Files[0]; var filename = Request.Form.FirstOrDefault(k => k.Key == "FileName").Value; var ext = Path.GetExtension(filename).ToLowerInvariant(); if (string.IsNullOrEmpty(ext) || !permittedExtensions.Contains(ext)) { return(BadRequest("Invalid file extension")); } try { using (var memoryStream = new MemoryStream()) { await formFile.CopyToAsync(memoryStream); var file = new AppFile { FileName = WebUtility.HtmlEncode(filename), OwnerId = User.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.NameIdentifier).Value, TopicId = int.Parse(Request.Form.FirstOrDefault(k => k.Key == "TopicId").Value), Content = memoryStream.ToArray() }; _context.FileList.Add(file); await _context.SaveChangesAsync(); } return(Created("File is uploaded", "")); } catch (DbUpdateException) { return(StatusCode(406)); } }
/// <summary> /// 删除角色时删除当前角色下的所有用户挂靠信息 /// </summary> /// <param name="Id"></param> /// <returns></returns> public async Task RemoveUserRoleByRoleId(int Id) { var userRoles = await _context.UserRole.Where(m => m.RoleId == Id).ToListAsync(); _context.UserRole.RemoveRange(userRoles); await _context.SaveChangesAsync(); }
public async Task <IActionResult> Put([FromRoute] string id, [FromBody] Study study) { _context.Studies.Update(study); await _context.SaveChangesAsync(); return(Ok(study)); }
public async Task <IActionResult> ModifyGroup(int id, [FromBody] Group modifiedGroup) { var group = await _context.GroupList.SingleOrDefaultAsync(g => g.Id == id); if (group == null) { return(BadRequest()); } var identified = await UserOwnsGroup(id); if (!identified) { return(Unauthorized("You do not have rights to do this")); } group.Title = modifiedGroup.Title; group.Description = modifiedGroup.Description; try { await _context.SaveChangesAsync(); return(Ok()); } catch (DbUpdateConcurrencyException) { return(StatusCode(505)); } }
public async Task <IActionResult> AddTopic(TopicPostRequest topicRequest) { try { Topic topic = new Topic() { Description = topicRequest.Description, Title = topicRequest.Title }; var userName = User.Identity.Name; var ownerId = User.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.NameIdentifier); topic.OwnerId = ownerId.Value; topic.OwnerName = userName; _context.TopicList.Add(topic); await _context.SaveChangesAsync(); return(Created("", "")); } catch (DbUpdateException e) { return(StatusCode(406)); } }
//创建用户 public async Task CreateAsync(User user, List <string> roleNames) { if (user == null) { throw new ArgumentNullException(nameof(user)); } _context.Users.Add(user); List <UserRole> userRoles = new List <UserRole>(); foreach (var roleName in roleNames) { UserRole userRole = new UserRole(); var role = await _context.Roles.FirstOrDefaultAsync(f => f.Name == roleName); userRole.RoleId = role.Id; userRoles.Add(userRole); user.UserRole = userRoles; foreach (var item in user.UserRole) { _context.UserRole.Add(item); } } await _context.SaveChangesAsync(); }
/// <summary> /// 移除权限 /// </summary> /// <param name="roleClaims"></param> public async Task Removes(List <RoleClaims> roleClaims) { _context.RoleClaims.RemoveRange(roleClaims); await _context.SaveChangesAsync(); }
/// <summary> /// 添加角色 /// </summary> /// <param name="role"></param> /// <returns></returns> public async Task AddRole(Role role) { _context.Roles.Add(role); await _context.SaveChangesAsync(); }