public async Task <SessionOutDto> Login(SessionCreateInDto loginParams) { #region login_user_if_existed_check var user = await _context.Users .Include(u => u.UserRoleAssociations) .ThenInclude(a => a.Role) .FirstOrDefaultAsync(u => u.Name == loginParams.Name); if (user == null) { throw new NotExistedException(); } #endregion #region login_user_password_check if (!ValidatePassword(user, loginParams.Password)) { throw new BadAuthenticationException(); } #endregion #region login_generate_jwt_bearer_token var roles = (from association in user.UserRoleAssociations select association.Role).ToList(); var permissions = await _permissionService.Filter(new PermissionFilterInDto { RoleIds = roles.Select(r => r.Id).ToList(), }); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Name), new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_authenticationService.ExpirationTime.TotalSeconds).ToString()) }; claims.AddRange(roles.Select(r => new Claim(ClaimTypes.Role, r.Name))); claims.AddRange(permissions.Select(p => new Claim(ClaimTypes.AuthorizationDecision, p.Name))); var token = _authenticationService.BuildJwtToken(claims); #endregion #region update_last_activate_info _context.Entry(user).State = EntityState.Modified; user.UpdateTime = DateTime.Now; await _context.SaveChangesAsync(); #endregion return(new SessionOutDto { User = user, Token = token }); }
public async Task <Comment> Update(Comment comment) { if (await _context.Comments.AnyAsync(c => c.Id == comment.Id)) { throw new NotExistedException(); } _context.Entry(comment).State = EntityState.Modified; _context.Entry(comment).Property(c => c.UserId).IsModified = false; _context.Entry(comment).Property(c => c.PostId).IsModified = false; comment.UpdateTime = DateTime.Now; await _context.SaveChangesAsync(); return(comment); }
public async Task <Post> Update(Post post) { if (!await _context.Posts.AnyAsync(p => p.Id == post.Id)) { throw new NotExistedException(); } _context.Entry(post).State = EntityState.Modified; if (post.PostTagAssociations != null) { _context.Entry(post.PostTagAssociations).State = EntityState.Modified; } post.UpdateTime = DateTime.Now; await _context.SaveChangesAsync(); return(post); }
public async Task <Application> Update(Application application) { if (null == await _context.Applications.FindAsync(application.Id)) { throw new NotExistedException(); } if (await _context.Applications.AnyAsync(a => a.Id != application.Id && a.Name == application.Name)) { throw new ExistedConflictException(); } _context.Entry(application).State = EntityState.Modified; _context.Entry(application.Profile).State = EntityState.Unchanged; if (application.Profile != null) { _context.Entry(application.Profile).State = EntityState.Modified; } application.UpdateTime = DateTime.Now; await _context.SaveChangesAsync(); return(application); }
public async Task <PostCategory> Update(PostCategory category) { if (!await _context.PostCategories.AnyAsync(t => t.Id == category.Id)) { throw new NotExistedException(); } if (!await _context.PostCategories.AnyAsync(t => t.Name == category.Name && t.Id != category.Id)) { throw new ExistedConflictException(); } _context.Entry(category).State = EntityState.Modified; await _context.SaveChangesAsync(); return(category); }
public async Task <PostTag> Update(PostTag tag) { if (!await _context.PostTags.AnyAsync(t => t.Id == tag.Id)) { throw new NotExistedException(); } if (!await _context.PostTags.AnyAsync(t => t.Name == tag.Name && t.Id != tag.Id)) { throw new ExistedConflictException(); } _context.Entry(tag).State = EntityState.Modified; await _context.SaveChangesAsync(); return(tag); }