Record Class UserContext

java.lang.Object
java.lang.Record
cloud.opencode.base.web.context.UserContext
Record Components:
userId - the user ID | 用户ID
username - the username | 用户名
roles - the user roles | 用户角色
permissions - the user permissions | 用户权限
attributes - additional attributes | 附加属性

public record UserContext(String userId, String username, Set<String> roles, Set<String> permissions, Map<String,Object> attributes) extends Record
User Context 用户上下文

Holds current user information for request processing.

保存当前用户信息用于请求处理。

Features | 主要功能:

  • Immutable user information record - 不可变用户信息记录
  • Role and permission checking - 角色和权限检查
  • Extensible attributes map - 可扩展的属性映射
  • Built-in anonymous and system user support - 内置匿名和系统用户支持

Usage Examples | 使用示例:

// Create user context
UserContext user = UserContext.of("123", "john");

// Check roles
boolean isAdmin = user.hasRole("ADMIN");
boolean hasAccess = user.hasAnyPermission("read", "write");

// System and anonymous users
UserContext system = UserContext.system();
UserContext anon = UserContext.anonymous();

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Partial (userId and username can be null for anonymous) - 空值安全: 部分(匿名用户的 userId 和 username 可为 null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • ANONYMOUS

      public static final UserContext ANONYMOUS
      Anonymous user constant 匿名用户常量
  • Constructor Details

  • Method Details

    • of

      public static UserContext of(String userId, String username)
      Create user context 创建用户上下文
      Parameters:
      userId - the user ID | 用户ID
      username - the username | 用户名
      Returns:
      the user context | 用户上下文
    • of

      public static UserContext of(String userId, String username, Set<String> roles)
      Create user context with roles 创建带角色的用户上下文
      Parameters:
      userId - the user ID | 用户ID
      username - the username | 用户名
      roles - the roles | 角色
      Returns:
      the user context | 用户上下文
    • hasRole

      public boolean hasRole(String role)
      Check if user has role 检查用户是否拥有角色
      Parameters:
      role - the role to check | 要检查的角色
      Returns:
      true if has role | 如果拥有角色返回true
    • hasAnyRole

      public boolean hasAnyRole(String... checkRoles)
      Check if user has any of the roles 检查用户是否拥有任一角色
      Parameters:
      checkRoles - the roles to check | 要检查的角色
      Returns:
      true if has any role | 如果拥有任一角色返回true
    • hasPermission

      public boolean hasPermission(String permission)
      Check if user has permission 检查用户是否拥有权限
      Parameters:
      permission - the permission to check | 要检查的权限
      Returns:
      true if has permission | 如果拥有权限返回true
    • hasAnyPermission

      public boolean hasAnyPermission(String... checkPermissions)
      Check if user has any of the permissions 检查用户是否拥有任一权限
      Parameters:
      checkPermissions - the permissions to check | 要检查的权限
      Returns:
      true if has any permission | 如果拥有任一权限返回true
    • getAttribute

      public <T> T getAttribute(String key)
      Get attribute 获取属性
      Type Parameters:
      T - the attribute type | 属性类型
      Parameters:
      key - the attribute key | 属性键
      Returns:
      the attribute value or null | 属性值或null
    • getAttribute

      public <T> T getAttribute(String key, T defaultValue)
      Get attribute with default value 获取属性(带默认值)
      Type Parameters:
      T - the attribute type | 属性类型
      Parameters:
      key - the attribute key | 属性键
      defaultValue - the default value | 默认值
      Returns:
      the attribute value or default | 属性值或默认值
    • isAnonymous

      public boolean isAnonymous()
      Check if anonymous user 检查是否匿名用户
      Returns:
      true if anonymous | 如果是匿名用户返回true
    • isAuthenticated

      public boolean isAuthenticated()
      Check if authenticated 检查是否已认证
      Returns:
      true if authenticated | 如果已认证返回true
    • anonymous

      public static UserContext anonymous()
      Get anonymous user context 获取匿名用户上下文
      Returns:
      the anonymous user context | 匿名用户上下文
    • system

      public static UserContext system()
      Get system user context 获取系统用户上下文
      Returns:
      the system user context | 系统用户上下文
    • isSuperAdmin

      public boolean isSuperAdmin()
      Check if user is super admin 检查是否超级管理员
      Returns:
      true if super admin | 如果是超级管理员返回true
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • userId

      public String userId()
      Returns the value of the userId record component.
      Returns:
      the value of the userId record component
    • username

      public String username()
      Returns the value of the username record component.
      Returns:
      the value of the username record component
    • roles

      public Set<String> roles()
      Returns the value of the roles record component.
      Returns:
      the value of the roles record component
    • permissions

      public Set<String> permissions()
      Returns the value of the permissions record component.
      Returns:
      the value of the permissions record component
    • attributes

      public Map<String,Object> attributes()
      Returns the value of the attributes record component.
      Returns:
      the value of the attributes record component