25 lines
1.2 KiB
C#
25 lines
1.2 KiB
C#
using Store.Users.Contracts.Enums;
|
|
using Store.Users.Entities;
|
|
|
|
namespace Store.Users.Services;
|
|
|
|
/// <summary>统一账户管理范围:管理员管理全部,主账号管理本人及直属子账号。</summary>
|
|
internal static class UserAccessPolicy
|
|
{
|
|
public static bool IsAdmin(UserEntity actor) => actor.SaLevel == UserAdminLevel.SuperAdmin;
|
|
|
|
public static bool Owns(UserEntity actor, UserEntity target) => actor.IsMainUser
|
|
&& string.Equals(target.ParentUserName, actor.UserName, StringComparison.OrdinalIgnoreCase);
|
|
|
|
public static bool CanRead(UserEntity actor, UserEntity target) =>
|
|
CanUseSelfService(actor, target);
|
|
|
|
/// <summary>子账号可查询本人、修改本人密码、提交充值备注和为本人店铺续费。</summary>
|
|
public static bool CanUseSelfService(UserEntity actor, UserEntity target) =>
|
|
actor.Id == target.Id || CanManage(actor, target);
|
|
|
|
/// <summary>管理员不受归属限制;普通主账号不能接管具有管理员身份的子账号。</summary>
|
|
public static bool CanManage(UserEntity actor, UserEntity target) =>
|
|
IsAdmin(actor) || (!IsAdmin(target) && actor.IsMainUser && (actor.Id == target.Id || Owns(actor, target)));
|
|
}
|