| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- # -*- coding: utf-8 -*-
- # @Author : YY
- import bcrypt
- from flask import abort
- from flask_login import current_user
- from ruoyi_common.constant import HttpStatus
- from ruoyi_common.domain.entity import LoginUser
- from ruoyi_common.utils.base import UtilException
- def get_user_id() -> int:
- """
- 获取当前登录用户的ID
- Raises:
- UtilException: 获取用户ID异常
- Returns:
- int: 当前登录用户的ID
- """
- try:
- return get_login_user().user_id
- except Exception:
- raise UtilException("获取用户ID异常", HttpStatus.UNAUTHORIZED)
- def get_dept_id() -> int:
- """
- 获取当前登录用户的部门ID
- Raises:
- UtilException: 获取部门ID异常
- Returns:
- int: 当前登录用户的部门ID
- """
- try:
- return get_login_user().dept_id
- except Exception:
- raise UtilException("获取部门ID异常", HttpStatus.UNAUTHORIZED)
- def get_username() -> str:
- """
- 获取当前登录用户的账户
- Raises:
- UtilException: 获取用户账户异常
- Returns:
- str: 当前登录用户的账户
- """
- try:
- return get_login_user().user_name
- except Exception as e:
- raise UtilException("获取用户账户异常", HttpStatus.UNAUTHORIZED)
- def get_login_user() -> LoginUser:
- """
- 获取当前登录用户的信息
- Raises:
- UtilException: 获取用户信息异常
- Returns:
- LoginUser: 当前登录用户的信息
- """
- try:
- # 检查是否有Flask-Login支持
- if hasattr(current_user, 'is_authenticated'):
- if not current_user.is_authenticated:
- abort(401)
- return current_user
- else:
- # 如果没有Flask-Login支持,返回None或抛出异常
- raise UtilException("获取用户信息异常", HttpStatus.UNAUTHORIZED)
- except Exception:
- raise UtilException("获取用户信息异常", HttpStatus.UNAUTHORIZED)
- def is_admin(user_id) -> bool:
- """
- 判断用户是否为管理员
- Args:
- user_id (int): 用户ID
- Returns:
- bool: 用户是否为管理员
- """
- return user_id is not None and user_id == 1
- def is_user_admin(user) -> bool:
- """
- 通过用户对象判断是否为管理员
-
- Args:
- user: 用户对象
-
- Returns:
- bool: 是否为管理员
- """
- # 检查是否为超级管理员用户
- if is_admin(user.user_id):
- return True
-
- # 检查用户角色中是否包含admin角色
- if hasattr(user, 'roles') and user.roles:
- for role in user.roles:
- if hasattr(role, 'role_key') and role.role_key == 'admin':
- return True
- return False
- def login_user_is_admin() -> bool:
- """
- 判断当前登录用户是否为管理员
- Returns:
- bool: 当前登录用户是否为管理员
- """
- try:
- user = get_login_user().user
- return is_user_admin(user)
- except:
- return is_admin(get_user_id())
|