Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean CurrentUserSource #497

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions src/Framework.SecuritySystem/UserSource/CurrentUserSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,9 @@

namespace Framework.SecuritySystem.UserSource;

public class CurrentUserSource<TUser> : ICurrentUserSource<TUser>
public class CurrentUserSource<TUser>(ICurrentUser currentUser, IUserSource<TUser> userSource) : ICurrentUserSource<TUser>
{
private readonly Lazy<TUser> lazyCurrentUser;

private readonly Lazy<Guid> lazyCurrentUserId;

public CurrentUserSource(
IUserSource<TUser> userSource,
UserPathInfo<TUser> userPathInfo,
ICurrentUser currentUser)
{
this.lazyCurrentUser = LazyHelper.Create(() => userSource.GetByName(currentUser.Name));

this.lazyCurrentUserId = LazyHelper.Create(() => userPathInfo.IdPath.Eval(this.CurrentUser));
}
private readonly Lazy<TUser> lazyCurrentUser = LazyHelper.Create(() => userSource.GetByName(currentUser.Name));

public TUser CurrentUser => this.lazyCurrentUser.Value;

public Guid CurrentUserId => this.lazyCurrentUserId.Value;
}
18 changes: 7 additions & 11 deletions src/Framework.SecuritySystem/UserSource/UserSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ public class UserSource<TUser>(IQueryableSource queryableSource, UserPathInfo<TU
{
public TUser? TryGetByName(string name) => this.GetQueryable(name).SingleOrDefault();

public TUser GetByName(string name)
{
return this.TryGetByName(name) ?? throw this.GetNotFoundException(name);
}
public TUser GetByName(string name) => this.TryGetByName(name) ?? throw this.GetNotFoundException(name);

public Guid? TryGetId(string name) =>
this.GetQueryable(name)
.Select(userPathInfo.IdPath)
.Select(v => (Guid?)v).SingleOrDefault();
public Guid? TryGetId(string name) => this.GetQueryable(name).Select(userPathInfo.IdPath).Select(v => (Guid?)v).SingleOrDefault();

public Guid GetId(string name) =>

this.TryGetId(name) ?? throw this.GetNotFoundException(name);

private IQueryable<TUser> GetQueryable(string name) => queryableSource.GetQueryable<TUser>()
.Where(userPathInfo.Filter)
.Where(userPathInfo.NamePath.Select(objName => objName == name));
private IQueryable<TUser> GetQueryable(string name) =>
queryableSource
.GetQueryable<TUser>()
.Where(userPathInfo.Filter)
.Where(userPathInfo.NamePath.Select(objName => objName == name));

private Exception GetNotFoundException(string name) => new UserSourceException($"{typeof(TUser).Name} \"{name}\" not found");
}
6 changes: 3 additions & 3 deletions src/__SolutionItems/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[assembly: AssemblyCompany("Luxoft")]
[assembly: AssemblyCopyright("Copyright © Luxoft 2009-2024")]

[assembly: AssemblyVersion("22.3.10.0")]
[assembly: AssemblyFileVersion("22.3.10.0")]
[assembly: AssemblyInformationalVersion("22.3.10.0")]
[assembly: AssemblyVersion("22.3.11.0")]
[assembly: AssemblyFileVersion("22.3.11.0")]
[assembly: AssemblyInformationalVersion("22.3.11.0")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
Expand Down
Loading