You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
class User:
|
|
|
|
|
def __init__(self, username, password, role='user'):
|
|
|
|
|
"""
|
|
|
|
|
初始化用户对象
|
|
|
|
|
:param username: 用户名
|
|
|
|
|
:param password: 密码(注意:在实际应用中不应明文存储,此处仅为示例)
|
|
|
|
|
:param role: 用户角色,如'user'(默认)、'admin'等
|
|
|
|
|
"""
|
|
|
|
|
self.username = username
|
|
|
|
|
self.password = password # 实际应用中应加密存储
|
|
|
|
|
self.role = role
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
"""返回用户的字符串表示"""
|
|
|
|
|
return f"用户名: {self.username}, 角色: {self.role}"
|
|
|
|
|
|
|
|
|
|
def change_password(self, new_password):
|
|
|
|
|
"""
|
|
|
|
|
更改用户密码
|
|
|
|
|
:param new_password: 新密码
|
|
|
|
|
"""
|
|
|
|
|
self.password = new_password # 实际应用中应处理密码加密逻辑
|
|
|
|
|
|
|
|
|
|
def promote_to_admin(self):
|
|
|
|
|
"""提升用户为管理员角色"""
|
|
|
|
|
self.role = 'admin'
|
|
|
|
|
|
|
|
|
|
def demote_to_user(self):
|
|
|
|
|
"""降级用户为普通用户角色"""
|
|
|
|
|
self.role = 'user'
|