Ejemplo n.º 1
0
def csu(dmp_username, real_name, passwd, email):
    """创建管理员用户"""
    from dmp.models import Users, Groups
    from dmp.utils.ep_data import EnvelopedData
    if not all([dmp_username, real_name, passwd, email]):
        return 'Insufficient parameter, please recreate superuser.'

    db_user_count = Users.query.count()
    if db_user_count == 0:
        user = Users(dmp_username=dmp_username,
                     real_name=real_name,
                     password=passwd,
                     email=email)
        rootgroup = Groups.query.filter(Groups.id == 1).first()
        user.dmp_group_id = 1
        user.leader_dmp_user_id = None
        user.confirmed = True
        db.session.add(user)
        db.session.commit()
        res = EnvelopedData.create_root(rootgroup)
        if isinstance(res, str):
            return {'msg': res}
        return {'msg': 'Super administrator has been created successfully.'}
    else:
        return {
            'msg':
            'The super administrator already exists in the database, please do not add it again.'
        }
Ejemplo n.º 2
0
def register(desc):
    '''
    说明:用户注册及超级管理员单一添加用户接口
    参数:dmp_username,real_name,password,email;说明:客户端请求参数信息,数据类型:JSON
    返回值:成功与失败返回对应的状态码及提示信息,数据类型:JSON,数据格式:{'msg':'pass','results':null,'status':xxx}
    '''
    try:
        user_obj = Users.query.filter_by(id=1).first()
        # 判断初始状态有没有超级用管理员,没有则不能创建用户,必须要先创建一个超级管理员
        ret = UserVerify.judge_superuser(user_obj)
        if ret:
            return resp_hanlder(code=999, msg=ret)
        data = request.json
        if data == None:
            return resp_hanlder(code=999)
        auth_token = data.get('Authorization')
        dmp_username = data.get('dmp_username')
        real_name = data.get('real_name')
        passwd = data.get('password')
        email = data.get('email')
        user = Users(dmp_username=dmp_username, real_name=real_name, password=passwd,
                     email=email, leader_dmp_user_id=1)
        res_token = PuttingData.get_obj_data(Users, auth_token)
        if auth_token != None and isinstance(res_token, dict):
            res = PuttingData.root_add_user(
                data, res_token, user, dmp_username, real_name)
            # 返回字典-管理员单一添加成功
            if isinstance(res, dict):
                return resp_hanlder(code=0, msg=res)

            # 返回元组-管理员/教师单一添加缺少参数
            elif isinstance(res, tuple):
                return resp_hanlder(code=999, msg=res[1])

            # 普通管理员和教师无法添加管理员角色,需要超级管理员添加
            elif res == -1:
                return resp_hanlder(code=999, msg='无法添加管理员用户组用户,请联系管理员添加.')
            elif res == -2:
                return resp_hanlder(code=999, msg='无法添加管理员或教师用户组用户,请联系管理员添加.')
        # 返回token错误的字符串-注册成功(注册时无token)
        db.session.add(user)
        db.session.commit()
        ValidationEmail().activate_email(user, email)
        return resp_hanlder(code=1001, msg=RET.alert_code[1001])
    except Exception as err:
        db.session.rollback()
        return resp_hanlder(code=999, msg=str(err))