Add PortraitUrl to User
This commit is contained in:
@@ -20,7 +20,7 @@ public class IdentityService(
|
||||
IAuthorizationService authorizationService,
|
||||
IHttpContextAccessor contextAccessor,
|
||||
IConfiguration configuration
|
||||
)
|
||||
)
|
||||
: IIdentityService
|
||||
{
|
||||
public async Task<string?> GetUserNameAsync(string userId)
|
||||
@@ -29,11 +29,11 @@ public class IdentityService(
|
||||
|
||||
return user?.UserName;
|
||||
}
|
||||
|
||||
|
||||
public async Task<UserModel?> GetUserByUserNameAsync(string userName)
|
||||
{
|
||||
var response = await userManager.FindByNameAsync(userName);
|
||||
|
||||
|
||||
if (response == null) return null;
|
||||
|
||||
var userModel = new UserModel()
|
||||
@@ -42,12 +42,13 @@ public class IdentityService(
|
||||
UserName = response.UserName,
|
||||
FirstName = response.FirstName,
|
||||
LastName = response.LastName,
|
||||
Email = response.Email,
|
||||
Email = response.Email,
|
||||
PortraitUrl = response.PortraitUrl,
|
||||
};
|
||||
|
||||
return userModel;
|
||||
}
|
||||
|
||||
|
||||
public async Task<Result<string>> CreateUserAsync(Userinfo userInfo)
|
||||
{
|
||||
var applicationUser = new ApplicationUser
|
||||
@@ -57,7 +58,7 @@ public class IdentityService(
|
||||
FirstName = userInfo.GivenName,
|
||||
LastName = userInfo.FamilyName
|
||||
};
|
||||
|
||||
|
||||
var password = Guid.NewGuid().ToString("N")[..32];
|
||||
|
||||
var identityResult = await userManager.CreateAsync(applicationUser, password);
|
||||
@@ -65,13 +66,14 @@ public class IdentityService(
|
||||
var applicationResult = identityResult.ToApplicationResult();
|
||||
|
||||
var result = new Result<string>(applicationResult.Succeeded, applicationResult.Errors);
|
||||
|
||||
|
||||
result.Value = applicationUser.Id;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Result<string>> CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
|
||||
|
||||
public async Task<Result<string>> CreateUserAsync(string email, string userName, string firstName, string lastName,
|
||||
string password)
|
||||
{
|
||||
var applicationUser = new ApplicationUser
|
||||
{
|
||||
@@ -82,16 +84,24 @@ public class IdentityService(
|
||||
};
|
||||
|
||||
var response = await userManager.CreateAsync(applicationUser, password);
|
||||
|
||||
|
||||
var result = new Result<string>(response.Succeeded, response.ToApplicationResult().Errors);
|
||||
result.Value = applicationUser.Id;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Result<string>> UpdateCurrentUserAsync(string id, string firstName, string lastName, string occupation,
|
||||
string phoneNumber, string birthDate, string country, string city, string address, string about, string description,
|
||||
SocialNetworksModel socialNetworks)
|
||||
public async Task<Result<string>> UpdateCurrentUserAsync(string id, string firstName, string lastName,
|
||||
string occupation,
|
||||
string phoneNumber,
|
||||
string birthDate,
|
||||
string country,
|
||||
string city,
|
||||
string address,
|
||||
string about,
|
||||
string description,
|
||||
SocialNetworksModel socialNetworks,
|
||||
string? portraitUrl)
|
||||
{
|
||||
var applicationUser = await userManager.FindByIdAsync(id);
|
||||
|
||||
@@ -107,6 +117,7 @@ public class IdentityService(
|
||||
applicationUser.Address = address;
|
||||
applicationUser.About = about;
|
||||
applicationUser.Description = description;
|
||||
applicationUser.PortraitUrl = portraitUrl;
|
||||
applicationUser.SocialNetworks = new SocialNetworks()
|
||||
{
|
||||
FacebookUrl = socialNetworks.FacebookUrl,
|
||||
@@ -130,7 +141,7 @@ public class IdentityService(
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public async Task<UserModel?> FindUserByIdAsync(string id)
|
||||
{
|
||||
var response = await userManager.FindByIdAsync(id);
|
||||
@@ -152,6 +163,7 @@ public class IdentityService(
|
||||
Address = response.Address,
|
||||
About = response.About,
|
||||
Description = response.Description,
|
||||
PortraitUrl = response.PortraitUrl,
|
||||
SocialNetworks = new SocialNetworksModel
|
||||
{
|
||||
FacebookUrl = response.SocialNetworks.FacebookUrl,
|
||||
@@ -167,7 +179,7 @@ public class IdentityService(
|
||||
|
||||
return userModel;
|
||||
}
|
||||
|
||||
|
||||
public async Task<UserModel?> GetCurrentUserAsync()
|
||||
{
|
||||
var currentUserId = contextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
@@ -203,7 +215,7 @@ public class IdentityService(
|
||||
|
||||
return user != null && await userManager.IsInRoleAsync(user, role);
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> CurrentUserIsInRoleAsync(string role)
|
||||
{
|
||||
var currentUserModel = await GetCurrentUserAsync();
|
||||
@@ -241,44 +253,45 @@ public class IdentityService(
|
||||
|
||||
return result.ToApplicationResult();
|
||||
}
|
||||
|
||||
|
||||
public async Task<Result> AddRoleAsync(string userId, string role)
|
||||
{
|
||||
var hasAdminAccess = await CurrentUserIsInRoleAsync("Administrator");
|
||||
|
||||
if (!hasAdminAccess) return Result.Failure(new []{"Only administrator can assign new roles to a user."});
|
||||
|
||||
if (!hasAdminAccess) return Result.Failure(new[] { "Only administrator can assign new roles to a user." });
|
||||
|
||||
var user = await userManager.FindByIdAsync(userId);
|
||||
|
||||
if (user is null) return Result.Failure(new []{"User not found."});
|
||||
|
||||
|
||||
if (user is null) return Result.Failure(new[] { "User not found." });
|
||||
|
||||
var result = await userManager.AddToRoleAsync(user, role);
|
||||
|
||||
return result.ToApplicationResult();
|
||||
}
|
||||
|
||||
|
||||
public async Task<IList<string>> GetCurrentUserRolesAsync()
|
||||
{
|
||||
var currentUserModel = await GetCurrentUserAsync();
|
||||
|
||||
|
||||
var currentUser = await userManager.FindByIdAsync(currentUserModel?.Id ?? "");
|
||||
|
||||
if (currentUser is null) return [];
|
||||
|
||||
if (currentUser is null) return [];
|
||||
|
||||
var userRoles = await userManager.GetRolesAsync(currentUser);
|
||||
|
||||
return userRoles;
|
||||
}
|
||||
|
||||
|
||||
public async Task<string?> LoginAsync(string userName, string password)
|
||||
{
|
||||
var result = await signInManager.PasswordSignInAsync(userName, password, isPersistent: false, lockoutOnFailure: false);
|
||||
|
||||
var result =
|
||||
await signInManager.PasswordSignInAsync(userName, password, isPersistent: false, lockoutOnFailure: false);
|
||||
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
var user = await GetUserByUserNameAsync(userName);
|
||||
|
||||
if (user is null) throw new InvalidOperationException();
|
||||
@@ -298,3 +311,6 @@ public class IdentityService(
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
public class ConfigurationMissingException(string ConfigurationKey)
|
||||
: Exception;
|
||||
|
||||
Reference in New Issue
Block a user