public override async Task <Profile> GetProfile(String username, String authedUsername = null)
        {
            Profile      profile     = new Profile(null, null, null, false);
            IdentityUser profileUser = null;

            try
            {
                profileUser = await _userManager.FindByNameAsync(username);

                profile.Username = profileUser.UserName;
                UserPersonalizationDAO userPersonalization = _userPersonalizationRepository.GetUserPersonalization(profileUser.Id);
                profile.Bio   = userPersonalization.Bio;
                profile.Image = userPersonalization.Image;
            } catch (Exception ex)
            {
                throw ex;
            }
            if (null != authedUsername)
            {
                try
                {
                    IdentityUser authedUser = await _userManager.FindByNameAsync(authedUsername);

                    string authedUserId = authedUser.Id;
                    profile.Following = _userIsFollowingRepository.IsUserFollowing(authedUserId, profileUser.Id);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(profile);
        }
Пример #2
0
        internal override async Task <User> UpdateUser(string username, UserUpdateData updateData)
        {
            var transaction = _context.Database.BeginTransaction();

            try
            {
                IdentityUser identityUser = await _userManager.FindByNameAsync(username);

                if (null != updateData.Username)
                {
                    var result = await _userManager.SetUserNameAsync(identityUser, updateData.Username);

                    if (!result.Succeeded)
                    {
                        throw new ConduitServerException($"Failed to update username of user with username {username}");
                    }
                }
                if (null != updateData.Email)
                {
                    var result = await _userManager.SetEmailAsync(identityUser, updateData.Email);

                    if (!result.Succeeded)
                    {
                        throw new ConduitServerException($"Failed to update email of user with username {username}");
                    }
                }
                if (null != updateData.Password)
                {
                    var passwordHash = _userManager.PasswordHasher.HashPassword(identityUser, updateData.Password);
                    identityUser.PasswordHash = passwordHash;
                    var result = await _userManager.UpdateAsync(identityUser);

                    if (!result.Succeeded)
                    {
                        throw new ConduitServerException($"Failed to update password of user with username {username}");
                    }
                }
                if (null != updateData.Bio)
                {
                    _userPersonalizationRepository.UpdateUserBio(identityUser.Id, updateData.Bio);
                }
                if (null != updateData.Image)
                {
                    _userPersonalizationRepository.UpdateUserImage(identityUser.Id, updateData.Image);
                }
                _context.SaveChanges();
                transaction.Commit();
                UserPersonalizationDAO userPersonalizationDTO = _userPersonalizationRepository.GetUserPersonalization(identityUser.Id);
                return(new User(identityUser.Email, GenerateJWTToken(identityUser), identityUser.UserName, userPersonalizationDTO.Bio, userPersonalizationDTO.Image));
            } catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
        }
Пример #3
0
        internal override async Task <User> GetCurrentUser(string username)
        {
            IdentityUser identityUser = await _userManager.FindByNameAsync(username);

            if (null == identityUser)
            {
                throw new ConduitNotFoundException($"Unable to find user with username {username}");
            }
            UserPersonalizationDAO userPersonalization = _userPersonalizationRepository.GetUserPersonalization(identityUser.Id);

            return(new User(identityUser.Email, GenerateJWTToken(identityUser), identityUser.UserName, userPersonalization.Bio, userPersonalization.Image));
        }
Пример #4
0
        internal override async Task <User> AuthenticateUser(string email, string password)
        {
            IdentityUser identityUser = await _userManager.FindByEmailAsync(email);

            SignInResult result = await _signInManager.CheckPasswordSignInAsync(identityUser, password, false);

            if (!result.Succeeded)
            {
                throw new ConduitUnauthorizedException($"Failed to sign in {email}");
            }
            UserPersonalizationDAO userPersonalization = _userPersonalizationRepository.GetUserPersonalization(identityUser.Id);

            return(new User(identityUser.Email, GenerateJWTToken(identityUser), identityUser.UserName, userPersonalization.Bio, userPersonalization.Image));
        }
        internal override void UpdateUserImage(string id, string image)
        {
            UserPersonalizationDAO userPersonalizationDTO = _userPersonalization.Where(e => e.UserId == id).Single();

            userPersonalizationDTO.Image = image;
        }
        internal override void UpdateUserBio(string id, string bio)
        {
            UserPersonalizationDAO userPersonalizationDTO = _userPersonalization.Where(e => e.UserId == id).Single();

            userPersonalizationDTO.Bio = bio;
        }