-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
153 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/UserProfilePictureProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using LINGYUN.Abp.Identity; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.BlobStoring; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Guids; | ||
using Volo.Abp.Identity; | ||
using Volo.Abp.Security.Claims; | ||
|
||
namespace LINGYUN.Abp.Account; | ||
|
||
[Dependency(ServiceLifetime.Transient, ReplaceServices = true)] | ||
[ExposeServices(typeof(IUserPictureProvider))] | ||
public class UserProfileUserPictureProvider : IUserPictureProvider | ||
{ | ||
protected IGuidGenerator GuidGenerator { get; } | ||
protected IdentityUserManager UserManager { get; } | ||
protected IBlobContainer<AccountContainer> AccountBlobContainer { get; } | ||
public UserProfileUserPictureProvider( | ||
IGuidGenerator guidGenerator, | ||
IdentityUserManager userManager, | ||
IBlobContainer<AccountContainer> accountBlobContainer) | ||
{ | ||
UserManager = userManager; | ||
GuidGenerator = guidGenerator; | ||
AccountBlobContainer = accountBlobContainer; | ||
} | ||
|
||
public async virtual Task SetPictureAsync(IdentityUser user, Stream stream, string fileName = null) | ||
{ | ||
var userId = user.Id.ToString("N"); | ||
var pictureBlobId = fileName ?? $"{GuidGenerator.Create():n}.jpg"; | ||
|
||
var avatarClaims = user.Claims.Where(x => x.ClaimType.StartsWith(AbpClaimTypes.Picture)) | ||
.Select(x => x.ToClaim()) | ||
.Skip(0) | ||
.Take(3) | ||
.ToList(); | ||
if (avatarClaims.Any()) | ||
{ | ||
// 保留最多3个头像 | ||
if (avatarClaims.Count >= 3) | ||
{ | ||
user.RemoveClaim(avatarClaims.First()); | ||
avatarClaims.RemoveAt(0); | ||
} | ||
|
||
// 历史头像加数字标识 | ||
for (var index = 1; index <= avatarClaims.Count; index++) | ||
{ | ||
var avatarClaim = avatarClaims[index - 1]; | ||
var findClaim = user.FindClaim(avatarClaim); | ||
if (findClaim != null) | ||
{ | ||
findClaim.SetClaim(new Claim( | ||
AbpClaimTypes.Picture + index.ToString(), | ||
findClaim.ClaimValue)); | ||
} | ||
} | ||
} | ||
|
||
user.AddClaim(GuidGenerator, new Claim(AbpClaimTypes.Picture, pictureBlobId)); | ||
|
||
(await UserManager.UpdateAsync(user)).CheckErrors(); | ||
|
||
var pictureName = $"{userId}/avatar/{pictureBlobId}"; | ||
|
||
await AccountBlobContainer.SaveAsync(pictureName, stream, true); | ||
} | ||
|
||
public async virtual Task<Stream> GetPictureAsync(string userId) | ||
{ | ||
var user = await UserManager.FindByIdAsync(userId); | ||
if (user == null) | ||
{ | ||
return Stream.Null; | ||
} | ||
|
||
var picture = user.Claims | ||
.FirstOrDefault(x => x.ClaimType == AbpClaimTypes.Picture) | ||
?.ClaimValue; | ||
|
||
if (picture.IsNullOrWhiteSpace()) | ||
{ | ||
return Stream.Null; | ||
} | ||
|
||
var pictureName = $"{user.Id:N}/avatar/{picture}"; | ||
|
||
return await AccountBlobContainer.ExistsAsync(pictureName) | ||
? await AccountBlobContainer.GetAsync(pictureName) | ||
: Stream.Null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...s/identity/LINGYUN.Abp.Identity.Domain/LINGYUN/Abp/Identity/DefaultUserPictureProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Identity; | ||
|
||
namespace LINGYUN.Abp.Identity; | ||
|
||
[Dependency(TryRegister = true)] | ||
public class DefaultUserPictureProvider : IUserPictureProvider, ISingletonDependency | ||
{ | ||
public Task SetPictureAsync(IdentityUser user, Stream stream, string fileName = null) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<Stream> GetPictureAsync(string userId) | ||
{ | ||
return Task.FromResult(Stream.Null); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...modules/identity/LINGYUN.Abp.Identity.Domain/LINGYUN/Abp/Identity/IUserPictureProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Identity; | ||
|
||
namespace LINGYUN.Abp.Identity; | ||
|
||
public interface IUserPictureProvider | ||
{ | ||
Task SetPictureAsync(IdentityUser user, Stream stream, string fileName = null); | ||
|
||
Task<Stream> GetPictureAsync(string userId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...entity/LINGYUN.Abp.Identity.QrCode/LINGYUN/Abp/Identity/QrCode/AbpIdentityQrCodeModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters