Bỏ qua, đến nội dung

@digiforce-nc/plugin-users

Plugin quản lý người dùng — định nghĩa collection users, cung cấp API quản lý tài khoản, profile, và tích hợp sâu với ACL và auth.

Plugin này làm gì?

plugin-userssổ danh bạ của hệ thống. Nó quản lý thông tin user (tên, email, phone, mật khẩu), cho phép admin tạo/sửa/xóa user, user tự cập nhật profile, và tự động gắn createdBy/updatedBy vào mọi bản ghi trong hệ thống.

Ba nhiệm vụ chính

#Nhiệm vụChi tiết
1Collection usersĐịnh nghĩa bảng users với UserModel, hỗ trợ desensitize (ẩn field nhạy cảm)
2API quản lýCRUD users (admin), updateProfile, updateLang, listExcludeRole, system settings
3Tự động hóaGắn createdBy/updatedBy vào collection khác, invalidate cache khi role thay đổi

Kiến trúc

Tổng quan

Thành phầnVai trò
UserModelModel class cho users, có desensitize() để ẩn field nhạy cảm (password...) khi trả về client
Custom actions5 action đặc biệt ngoài CRUD chuẩn
Auto-inject fieldsTự thêm createdBy/updatedBy vào collection có interface tương ứng

Luồng updateProfile

Bảo mật

updateProfile chỉ cho phép sửa field được định nghĩa trong UI schema profile form. User không thể tự sửa roles, password (dùng changePassword), hay field hệ thống.

API endpoints

CRUD chuẩn

EndpointMô tả
users:listDanh sách user (admin)
users:getChi tiết user
users:createTạo user mới
users:updateCập nhật user
users:destroyXóa user (không xóa được user id=1)

Custom actions

EndpointAi dùngMô tả
users:updateProfileUser đăng nhậpCập nhật profile cá nhân (chỉ field được phép)
users:updateLangUser đăng nhậpĐổi ngôn ngữ giao diện
users:listExcludeRoleAdminDanh sách user chưa có role cụ thể (dùng khi gán role)
users:getSystemSettingsAdminLấy cấu hình profile/password
users:updateSystemSettingsAdminCập nhật cấu hình profile/password

Database

Bảng users

FieldTypeMô tả
idbigInt (PK)ID user
displaynamestringTên hiển thị
usernamestring (unique)Tên đăng nhập
emailstring (unique)Email
phonestring (unique)Số điện thoại
passwordpassword (hidden)Mật khẩu (bcrypt hash, ẩn khỏi API)
appLangstringNgôn ngữ giao diện
passwordChangeTzstringThời điểm đổi password gần nhất
resetTokenstringToken reset password
systemSettingsjsonCài đặt cá nhân

System settings (extended)

Plugin mở rộng bảng systemSettings thêm 2 flag:

FieldMô tả
enableEditProfileCho phép user tự sửa profile
enableChangePasswordCho phép user tự đổi mật khẩu

Tự động gắn createdBy / updatedBy

Khi collection nào có createdBy: true hoặc updatedBy: true (hoặc dùng interface tương ứng), plugin tự động thêm field liên kết đến users:

typescript
// Ví dụ collection orders có createdBy
{
  name: 'orders',
  createdBy: true,
  updatedBy: true,
  fields: [
    { name: 'title', type: 'string' },
    // createdBy, updatedBy được tự động thêm bởi plugin-users
  ]
}

Khi tạo/sửa record, hệ thống tự gán createdById / updatedById = user hiện tại.

DB operators

Plugin đăng ký các operator đặc biệt cho filter:

OperatorMô tảVí dụ
$isCurrentUserRecord thuộc user hiện tại{ createdById: { $isCurrentUser: true } }
$isNotCurrentUserRecord không thuộc user hiện tại{ createdById: { $isNotCurrentUser: true } }
$isVarSo sánh với biến hệ thốngFilter động

Thành phần client

Thành phầnMô tả
UsersManagementTrang quản lý users (Settings → Users & Permissions → Users)
RoleUsersManagerTab gán user vào role (trong trang Roles & Permissions)
PreferencesUserCenterMenu user: đổi ngôn ngữ, đổi role, sửa profile
UpdateProfilePreferenceForm sửa profile cá nhân
PasswordPreferenceForm đổi mật khẩu
LanguagePreferenceĐổi ngôn ngữ
RolePreferenceĐổi role mặc định
SignOutNút đăng xuất

Dependencies

PackageVai trò
@digiforce-nc/plugin-aclSnippets, role cache, fixed params bảo vệ user root
@digiforce-nc/plugin-authXác thực user, cache auth
@digiforce-nc/plugin-system-settingsLưu cấu hình profile/password
@digiforce-nc/plugin-ui-schema-storageUI schema cho profile form
@digiforce-nc/plugin-user-data-syncTùy chọn: đồng bộ user từ nguồn bên ngoài
@digiforce-nc/serverServer framework
@digiforce-nc/databaseDatabase ORM
@digiforce-nc/clientClient framework

Mục lục chi tiết

  • Kiến trúc — UserModel, operators, auto fields, extension
  • API Reference — CRUD, custom actions, filter operators, system settings
  • Database Schema — ER diagram, bảng users, systemSettings
  • FAQ — Câu hỏi thường gặp về quản lý người dùng