Skip to content

Commit

Permalink
Fixed user store.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Mar 7, 2015
1 parent 39aaa49 commit c158885
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
83 changes: 43 additions & 40 deletions src/Abp.Zero/Authorization/Users/AbpUserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,65 +70,67 @@ public async Task<TUser> FindByIdAsync(long userId)
public async Task<TUser> FindByNameAsync(string userName)
{
return await _userRepository.FirstOrDefaultAsync(
user =>
user.TenantId == _session.TenantId && //TODO: Should filter automatically when ABP implements it
(user.UserName == userName || user.EmailAddress == userName) &&
user.IsEmailConfirmed
user => user.TenantId == _session.TenantId && user.UserName == userName
);
}

public async Task SetPasswordHashAsync(TUser user, string passwordHash)
public async Task<TUser> FindByEmailAsync(string email)
{
user.Password = passwordHash;
if (!user.IsTransient())
{
await _userRepository.UpdateAsync(user);
}
return await _userRepository.FirstOrDefaultAsync(
user => user.EmailAddress == email && user.TenantId == _session.TenantId
);
}

public async Task<string> GetPasswordHashAsync(TUser user)
/// <summary>
/// Tries to find a user with user name or email address.
/// </summary>
/// <param name="userNameOrEmailAddress">User name or email address</param>
/// <returns>User or null</returns>
public async Task<TUser> FindByNameOrEmailAsync(string userNameOrEmailAddress)
{
return (await _userRepository.GetAsync(user.Id)).Password;
return await _userRepository.FirstOrDefaultAsync(
user =>
user.TenantId == _session.TenantId &&
(user.UserName == userNameOrEmailAddress || user.EmailAddress == userNameOrEmailAddress)
);
}

public async Task<bool> HasPasswordAsync(TUser user)
public Task SetPasswordHashAsync(TUser user, string passwordHash)
{
return !string.IsNullOrEmpty((await _userRepository.GetAsync(user.Id)).Password);
user.Password = passwordHash;
return Task.FromResult(0);
}

public async Task SetEmailAsync(TUser user, string email)
public Task<string> GetPasswordHashAsync(TUser user)
{
user.EmailAddress = email;
if (!user.IsTransient())
{
await _userRepository.UpdateAsync(user);
}
return Task.FromResult(user.Password);
}

public async Task<string> GetEmailAsync(TUser user)
public Task<bool> HasPasswordAsync(TUser user)
{
return (await _userRepository.GetAsync(user.Id)).EmailAddress;
return Task.FromResult(!string.IsNullOrEmpty(user.Password));
}

public async Task<bool> GetEmailConfirmedAsync(TUser user)
public Task SetEmailAsync(TUser user, string email)
{
return (await _userRepository.GetAsync(user.Id)).IsEmailConfirmed;
user.EmailAddress = email;
return Task.FromResult(0);
}

public async Task SetEmailConfirmedAsync(TUser user, bool confirmed)
public Task<string> GetEmailAsync(TUser user)
{
user.IsEmailConfirmed = confirmed;
if (!user.IsTransient())
{
await _userRepository.UpdateAsync(user);
}
return Task.FromResult(user.EmailAddress);
}

public async Task<TUser> FindByEmailAsync(string email)
public Task<bool> GetEmailConfirmedAsync(TUser user)
{
return await _userRepository.FirstOrDefaultAsync(
user => user.EmailAddress == email && user.TenantId == _session.TenantId
);
return Task.FromResult(user.IsEmailConfirmed);
}

public Task SetEmailConfirmedAsync(TUser user, bool confirmed)
{
user.IsEmailConfirmed = confirmed;
return Task.FromResult(0);
}

public async Task AddLoginAsync(TUser user, UserLoginInfo login)
Expand Down Expand Up @@ -168,17 +170,18 @@ public async Task<TUser> FindAsync(UserLoginInfo login)
return null;
}

return await _userRepository.FirstOrDefaultAsync(userLogin.UserId);
return await _userRepository.FirstOrDefaultAsync(u => u.Id == userLogin.UserId && u.TenantId == _session.TenantId);
}

public async Task AddToRoleAsync(TUser user, string roleName)
{
var role = await _roleRepository.SingleAsync(r => r.Name == roleName && r.TenantId == _session.TenantId);
await _userRoleRepository.InsertAsync(new UserRole
{
UserId = user.Id,
RoleId = role.Id
});
await _userRoleRepository.InsertAsync(
new UserRole
{
UserId = user.Id,
RoleId = role.Id
});
}

public async Task RemoveFromRoleAsync(TUser user, string roleName)
Expand Down
2 changes: 1 addition & 1 deletion src/Abp.Zero/Zero/AbpZeroCoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class AbpZeroCoreModule : AbpModule
/// <summary>
/// Current version of the zero module.
/// </summary>
public const string CurrentVersion = "0.5.5.0";
public const string CurrentVersion = "0.5.5.1";

public override void PreInitialize()
{
Expand Down

0 comments on commit c158885

Please sign in to comment.