Subject

Subject

主体类,实例指向当前会话的具体用户,通过它来提供认证和授权信息的存取

Constructor

new Subject(ctx, 会话管理器的实例)

Source:
Parameters:
Name Type Description
ctx object egg's ctx
会话管理器的实例 SessionManager

Members

ctx

Source:
egg 的 ctx 对象,通过它来与 session 或其他 cache 打交道,实现数据的存取

isLogined :boolean

Source:
是否登录
Type:
  • boolean

permissions :object

Source:
主体所拥有的权限
Type:
  • object

roleNames :array.<string>

Source:
主体所属的角色名称
Type:
  • array.<string>

sessionManager

Source:
会话管理器对象,内部通过它来找到会话 id,进而通过 id 去 storage 中找到 token

Methods

(async) getRealm() → {Realm}

Source:
获取数据源实例
Returns:
Type
Realm

(async) getToken() → {AuthenticationToken}

Source:
获取认证 token,默认从 session 中拿到并构造一个 AuthenticationToken 的实例返回
Returns:
认证 token 的实例,或者 null
Type
AuthenticationToken

hasPermission(permission)

Source:
是否拥有指定权限
Parameters:
Name Type Description
permission string 权限名称

hasPermissions(permissions)

Source:
是否拥有指定权限集
Parameters:
Name Type Description
permissions array.<string> 权限名称集合

hasRole(roleName)

Source:
主体是否拥有指定角色
Parameters:
Name Type Description
roleName string 角色名称

hasRoles(roleNames)

Source:
主体是否拥有指定多个角色
Parameters:
Name Type Description
roleNames array.<string> 角色名称集合

(async) init()

Source:
初始化方法,在构造完成后会调用,函数内拿到会话对应的 token 异步完成角色和权限数据的获取与设置

(async) login(token) → {object}

Source:
See:
主体登录
Parameters:
Name Type Description
token AuthenticationToken 认证 token 的实例
Throws:
Returns:
认证成功后拿到的用户信息
Type
object

(async) logout()

Source:
登出

setPermissions(permissions)

Source:
设置主体的权限列表
Parameters:
Name Type Description
permissions array.<string> 权限数组

setRealm(realm)

Source:
设置数据源实例
Parameters:
Name Type Description
realm Realm 数据源

setRoles(roleNames)

Source:
设置主体的角色
Parameters:
Name Type Description
roleNames array.<string> 角色名称

(async) setToken(token)

Source:
设置认证token,内部完成 token 的存储,默认存储在 session 中
Parameters:
Name Type Description
token AuthenticationToken