def add(cls, **kwargs): existed = User.get_by(username=kwargs['username'], email=kwargs['email']) existed and abort( 400, "User <{0}> is already existed".format(kwargs['username'])) kwargs['nickname'] = kwargs.get('nickname') or kwargs['username'] kwargs['block'] = 0 kwargs['key'], kwargs['secret'] = cls._gen_key_secret() user = User.create(**kwargs) RoleCRUD.add_role(user.username, uid=user.uid) return user
def delete(cls, uid): user = User.get_by(uid=uid, to_dict=False, first=True) or abort( 404, "User <{0}> does not exist".format(uid)) UserCache.clean(user) user.soft_delete()
def clean_db(): """clean all data but not drop table""" for table in reversed(db.metadata.sorted_tables): if table.fullname in ["users"]: continue db.session.execute(table.delete()) db.session.commit() if not User.get_by(email="*****@*****.**"): print("hello world xxxxx") u = User.create(flush=True, username="******", nickname="测试", email="*****@*****.**", key="", secret="") u._set_password("123456") u.save()
def delete(cls, uid): if uid == g.user.uid: return abort(400, "You cannot delete yourself") user = User.get_by(uid=uid, to_dict=False, first=True) or abort( 404, "User <{0}> does not exist".format(uid)) UserCache.clean(user) user.soft_delete()
def del_user(user): """ delete a user Example: flask del-user -u <username> """ assert user is not None from api.models.acl import User u = User.get_by(username=user, first=True, to_dict=False) u and UserCRUD.delete(u.uid)
def update(uid, **kwargs): user = User.get_by(uid=uid, to_dict=False, first=True) or abort( 404, "User <{0}> does not exist".format(uid)) if kwargs.get("username"): other = User.get_by(username=kwargs['username'], first=True, to_dict=False) if other is not None and other.uid != user.uid: return abort( 400, "User <{0}> cannot be duplicated".format( kwargs['username'])) UserCache.clean(user) if kwargs.get("username") and kwargs['username'] != user.username: role = Role.get_by(name=user.username, first=True, to_dict=False) if role is not None: RoleCRUD.update_role(role.id, **dict(name=kwargs['name'])) return user.update(**kwargs)
def delete(cls, uid): if hasattr(g, 'user') and uid == g.user.uid: return abort(400, "You cannot delete yourself") user = User.get_by(uid=uid, to_dict=False, first=True) or abort( 404, "User <{0}> does not exist".format(uid)) UserCache.clean(user) for i in Role.get_by(uid=uid, to_dict=False): i.delete() user.delete()
def add(cls, **kwargs): existed = User.get_by(username=kwargs['username'], email=kwargs['email']) existed and abort( 400, "User <{0}> is already existed".format(kwargs['username'])) is_admin = kwargs.pop('is_admin', False) kwargs['nickname'] = kwargs.get('nickname') or kwargs['username'] kwargs['block'] = 0 kwargs['key'], kwargs['secret'] = cls._gen_key_secret() user = User.create(**kwargs) role = RoleCRUD.add_role(user.username, uid=user.uid) if is_admin: from api.lib.perm.acl.cache import AppCache from api.lib.perm.acl.role import RoleRelationCRUD admin_r = Role.get_by(name='admin', first=True, to_dict=False) or \ RoleCRUD.add_role('admin', AppCache.get('cmdb').id, True) RoleRelationCRUD.add(admin_r.id, role.id) return user
def load_user(user_id): """Load user by ID.""" return User.get_by(uid=int(user_id), first=True, to_dict=False)