예제 #1
0
def register(json: UserRegisterSchema):
    """
    注册新用户
    """
    if manager.user_model.count_by_username(g.username) > 0:
        raise Duplicated("用户名重复,请重新输入")  # type: ignore
    if g.email and g.email.strip() != "":
        if manager.user_model.count_by_email(g.email) > 0:
            raise Duplicated("注册邮箱重复,请重新输入")  # type: ignore
    # create a user
    with db.auto_commit():
        user = manager.user_model()
        user.username = g.username
        if g.email and g.email.strip() != "":
            user.email = g.email
        db.session.add(user)
        db.session.flush()
        user.password = g.password
        group_ids = g.group_ids
        # 如果没传分组数据,则将其设定为 guest 分组
        if len(group_ids) == 0:
            from lin import GroupLevelEnum

            group_ids = [GroupLevelEnum.GUEST.value]
        for group_id in group_ids:
            user_group = manager.user_group_model()
            user_group.user_id = user.id
            user_group.group_id = group_id
            db.session.add(user_group)

    return Success("用户创建成功")  # type: ignore
예제 #2
0
def update_user(uid: int, json: UpdateUserInfoSchema):
    """
    更新用户信息
    """
    user = manager.user_model.get(id=uid)
    if user is None:
        raise NotFound("用户不存在")
    if user.email != g.email:
        exists = manager.user_model.get(email=g.email)
        if exists:
            raise ParameterError("邮箱已被注册,请重新输入邮箱")
    with db.auto_commit():
        user.email = g.email
        group_ids = g.group_ids
        # 清空原来的所有关联关系
        manager.user_group_model.query.filter_by(user_id=user.id).delete(
            synchronize_session=False)
        # 根据传入分组ids 新增关联记录
        user_group_list = list()
        # 如果没传分组数据,则将其设定为 guest 分组
        if not group_ids:
            group_ids = [
                manager.group_model.get(level=GroupLevelEnum.GUEST.value).id
            ]
        for group_id in group_ids:
            user_group = manager.user_group_model()
            user_group.user_id = user.id
            user_group.group_id = group_id
            user_group_list.append(user_group)
        db.session.add_all(user_group_list)
    return Success("操作成功")
예제 #3
0
파일: init.py 프로젝트: aodic/jw-flask
def init(force=False):
    db.create_all()
    if force:
        db.drop_all()
        db.create_all()
    elif (manager.user_model.get(one=False)
          or manager.user_group_model.get(one=False)
          or manager.group_model.get(one=False)):
        exit("表中存在数据,初始化失败")
    with db.auto_commit():
        # 创建一个超级管理员分组
        root_group = manager.group_model()
        root_group.name = "Root"
        root_group.info = "超级用户组"
        root_group.level = GroupLevelEnum.ROOT.value
        db.session.add(root_group)
        # 创建一个超级管理员
        root = manager.user_model()
        root.username = "******"
        db.session.add(root)
        db.session.flush()
        root.password = "******"
        # root用户 and  超级管理员分组 对应关系写入user_group表中
        user_group = manager.user_group_model()
        user_group.user_id = root.id
        user_group.group_id = root_group.id
        db.session.add(user_group)
        # 添加 默认游客组
        guest_group = manager.group_model()
        guest_group.name = "Guest"
        guest_group.info = "游客组"
        guest_group.level = GroupLevelEnum.GUEST.value
        db.session.add(guest_group)
        # 初始化权限
        manager.sync_permissions()
예제 #4
0
파일: admin.py 프로젝트: aodic/jw-flask
def update_user(uid):
    form = UpdateUserInfoForm().validate_for_api()

    user = manager.user_model.get(id=uid)
    if user is None:
        raise NotFound("用户不存在")
    if user.email != form.email.data:
        exists = manager.user_model.get(email=form.email.data)
        if exists:
            raise ParameterError("邮箱已被注册,请重新输入邮箱")
    with db.auto_commit():
        user.email = form.email.data
        group_ids = form.group_ids.data
        # 清空原来的所有关联关系
        manager.user_group_model.query.filter_by(user_id=user.id).delete(
            synchronize_session=False)
        # 根据传入分组ids 新增关联记录
        user_group_list = list()
        # 如果没传分组数据,则将其设定为 guest 分组
        if len(group_ids) == 0:
            group_ids = [
                manager.group_model.get(level=GroupLevelEnum.GUEST.value).id
            ]
        for group_id in group_ids:
            user_group = manager.user_group_model()
            user_group.user_id = user.id
            user_group.group_id = group_id
            user_group_list.append(user_group)
        db.session.add_all(user_group_list)
    return Success("操作成功")
예제 #5
0
파일: user.py 프로젝트: aodic/jw-flask
def _register_user(form: RegisterForm):
    with db.auto_commit():
        user = manager.user_model()
        user.username = form.username.data
        if form.email.data and form.email.data.strip() != "":
            user.email = form.email.data
        db.session.add(user)
        db.session.flush()
        user.password = form.password.data
        group_ids = form.group_ids.data
        # 如果没传分组数据,则将其设定为 id 2 的 guest 分组
        if len(group_ids) == 0:
            group_ids = [2]
        for group_id in group_ids:
            user_group = manager.user_group_model()
            user_group.user_id = user.id
            user_group.group_id = group_id
            db.session.add(user_group)