Skip to content

feat: enhance user permission handling and add account status checks#573

Merged
zds-s merged 1 commit into
masterfrom
patch-2
Mar 20, 2025
Merged

feat: enhance user permission handling and add account status checks#573
zds-s merged 1 commit into
masterfrom
patch-2

Conversation

@zds-s

@zds-s zds-s commented Mar 20, 2025

Copy link
Copy Markdown
Member

fixed

Summary by CodeRabbit

  • 新功能
    • 优化权限验证:当账户处于停用状态时,系统会显示明确的提示信息(包括“Account deactivated”、“账号已停用”和“賬號已停用”),帮助用户了解当前账户状态。
    • 超级管理员优化:超级管理员现在将直接获得访问权限,无需经过标准权限验证流程。

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 20, 2025
@coderabbitai

coderabbitai Bot commented Mar 20, 2025

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

该 PR 修改了 PermissionMiddleware 类中的 process 方法,为用户状态和角色判断引入了新的逻辑。现在会先检查用户是否处于禁用状态,若是则抛出异常;若用户是超级管理员,则跳过权限验证,直接处理请求。同时,在 Status 枚举中增加了 isNormal()isDisable() 方法以便判断用户状态。docker-compose.yml 中调整了 volume 挂载和工作目录。此外,多处语言包中新增了账号禁用相关的提示信息。

Changes

文件 修改摘要
app/.../PermissionMiddleware.php 修改 process 方法:新增获取用户变量,检查用户禁用状态(抛出 BusinessException),对超级管理员直接放行,移除 handlePermission 中的超级管理员检查。
app/.../Status.php Status 枚举新增 public function isNormal(): boolpublic function isDisable(): bool 方法,便于判断状态。
docker-compose.yml 更新 hyperf 服务的 volume 映射由 ./:/www/MineAdmin 改为 ./:/www,工作目录由 /www/MineAdmin 改为 /www
storage/languages/.../user.php (en, zh_CN, zh_TW) 为各语言包新增 'disable' 键:英文为 "Account deactivated",中文(简体)为 "账号已停用",中文(繁体)为 "賬號已停用"。

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant M as PermissionMiddleware
    participant U as User
    participant H as Request Handler

    C->>M: 发起请求
    M->>U: 获取当前用户
    U-->>M: 返回用户对象
    M->>M: 检查用户是否禁用?
    alt 用户被禁用
        M->>M: 抛出 BusinessException
    else 用户未禁用
        M->>M: 判断是否为超级管理员?
        alt 超级管理员
            M->>H: 直接转发请求
        else 普通用户
            M->>M: 进行权限验证
            M->>H: 转发经过权限验证的请求
        end
    end
Loading

Possibly related PRs

Suggested labels

enhancement, size:M

Poem

在代码的森林中奔跑,
我这小兔轻巧跳跃;
权限检查如同晨曦亮,
禁用提示添彩斑斓;
编程之旅乐趣无穷 🐰💻
—— 代码兔的颂歌

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or PR title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dosubot dosubot Bot added the enhancement label Mar 20, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
app/Model/Enums/User/Status.php (1)

30-38: 添加了状态检查的便捷方法,增强了枚举类的实用性。

这两个新方法提供了检查用户状态的便捷方式,使代码更具可读性和维护性。建议考虑枚举常量命名风格的一致性,目前 Normal 使用首字母大写,而 DISABLE 使用全大写,可以统一为一种风格。

- case DISABLE = 2;
+ case Disable = 2;

同时,相应的 isDisable() 方法中的比较也需要更新:

- return $this === self::DISABLE;
+ return $this === self::Disable;
docker-compose.yml (1)

44-45: 更新了卷挂载路径和工作目录配置

这个变更将容器中的挂载点从更具体的 /www/MineAdmin 改为更宽泛的 /www,同时工作目录也做了相应调整。这种变更可能影响容器内应用程序的路径解析和文件访问行为。

请确认以下几点:

  1. 应用程序在新路径配置下能否正常运行
  2. 在项目文档中更新相关的开发环境设置说明
  3. 考虑添加注释说明为什么进行这项更改,以便团队其他成员理解变更的目的
  hyperf:
    image: hyperf/hyperf:8.1-alpine-v3.18-swoole
    volumes:
-      - ./:/www/MineAdmin
+      - ./:/www
-    working_dir: /www/MineAdmin
+    working_dir: /www
    ports:
app/Http/Admin/Middleware/PermissionMiddleware.php (1)

38-44: 增强了用户权限和状态检查逻辑

这段代码增加了两项重要的检查:1) 用户账号是否被禁用;2) 用户是否为超级管理员。这种实现方式既提高了系统安全性,又优化了超级管理员的访问性能。

代码改进非常合理,通过在中间件层面实现用户状态检查和超级管理员权限判断,可以更有效地管理用户访问权限。特别是对于被禁用账号的处理,增加了系统安全性。

建议考虑为超级管理员直接返回处理结果的情况添加日志记录,以便于后期审计和调试:

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $user = $this->currentUser->user();
        if ($user->status->isDisable()) {
            throw new BusinessException(code: ResultCode::FORBIDDEN, message: trans('user.disable'));
        }
        if ($user->isSuperAdmin()) {
+           // 记录超级管理员的访问日志
+           logger('admin')->info('SuperAdmin access', ['user_id' => $user->id, 'path' => $request->getUri()->getPath()]);
            return $handler->handle($request);
        }
        $this->check($request->getAttribute(Dispatched::class));
        return $handler->handle($request);
    }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1625566 and a468014.

📒 Files selected for processing (6)
  • app/Http/Admin/Middleware/PermissionMiddleware.php (1 hunks)
  • app/Model/Enums/User/Status.php (1 hunks)
  • docker-compose.yml (1 hunks)
  • storage/languages/en/user.php (1 hunks)
  • storage/languages/zh_CN/user.php (1 hunks)
  • storage/languages/zh_TW/user.php (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
app/Http/Admin/Middleware/PermissionMiddleware.php (3)
app/Http/CurrentUser.php (2) (2)
  • user (33-36)
  • isSuperAdmin (48-51)
app/Model/Enums/User/Status.php (1) (1)
  • isDisable (35-38)
app/Model/Permission/User.php (1) (1)
  • isSuperAdmin (114-117)
⏰ Context from checks skipped due to timeout of 90000ms (19)
  • GitHub Check: Test on PHP8.2 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.2 Swoole-master
  • GitHub Check: Test on PHP8.2 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.1 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.3 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.3 Swoole-master
  • GitHub Check: Test on PHP8.1 Swow-develop
  • GitHub Check: Test on PHP8.3 Swow-develop
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: build Code coverage report (ubuntu-latest, 8.1, v5.1.3)
  • GitHub Check: Test on PHP8.1 Swoole-master
  • GitHub Check: Test on PHP8.1 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.1 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.2 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.2 Swoole-master
  • GitHub Check: Test on PHP8.3 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.3 Swoole-master
  • GitHub Check: Test on PHP8.2 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.2 Swow-develop
🔇 Additional comments (3)
storage/languages/zh_CN/user.php (1)

42-42: 增加了禁用账号的提示信息,提升了用户体验。

此修改合理地添加了账号禁用状态的提示信息,与系统新增的用户状态检查功能相匹配,使用户在账号被禁用时能够看到清晰的提示。

storage/languages/zh_TW/user.php (1)

43-43: 增加了繁體中文版本的账号禁用提示信息。

此修改为繁体中文用户提供了账号禁用状态的提示信息,与简体中文和英文版本保持一致,确保了多语言环境下的用户体验一致性。

storage/languages/en/user.php (1)

42-42: 添加了英文版本的账号禁用提示信息。

此修改为英文界面添加了账号禁用状态的提示信息,与中文版本保持一致,完善了国际化支持。

@zds-s
zds-s merged commit aa508ba into master Mar 20, 2025
@zds-s
zds-s deleted the patch-2 branch March 20, 2025 10:01
@codecov

codecov Bot commented Mar 20, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (1625566) to head (a468014).
Report is 1 commits behind head on master.

Additional details and impacted files
@@      Coverage Diff      @@
##   master   #573   +/-   ##
=============================
=============================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant