Example #1
0
    def post(self, **kwargs):
        args = user_create_parser.parse_args()
        username, password = args["username"], args["password"]
        role, active = args["role"], args["active"]
        balance = args["balance"]
        active = active == "true"
        salt = app.config.get("SALT", b"")
        password = bcrypt.hashpw(password.encode('utf8'), bytes(salt.encode()))
        status = "OK"
        user_id = ""

        try:
            user = User(username,
                        password,
                        is_admin=role == ADMIN,
                        role=role,
                        active=active,
                        balance=balance)
            user.save()
            user_id = user.id
        except Exception as exc:
            logger.error("exc %s", exc)
            status = "FAIL"

        return {"status": status, "id": user_id}, 200
Example #2
0
    def post(self):
        args = register_parser.parse_args()
        mobile = args.get('mobile')
        email = args.get('email')

        # 验证手机格式
        if not re.match('^1[3456789]\d{9}$', mobile):
            return {'stat': '400', 'msg': '手机号格式错误'}
        if not re.match(
                '^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$',
                email):
            return {'stat': '400', 'msg': '邮箱格式错误'}

        # 验证手机是否存在
        try:
            user = User().get_by_mobile(mobile)
            if user:
                return {'stat': '400', 'msg': '该手机号已提交申请,请耐心等待审核!'}
        except Exception as e:
            logger.error(e)
            return {'stat': '400', 'msg': '读取数据库错误'}

        # 存储用户
        args['password'] = mobile[-6:]
        try:
            user = User(**args, role=1)
            user.save()
        except Exception as e:
            logger.error(e)
            return {'stat': '400', 'msg': '数据库存储错误'}

        data = {"stat": 200, 'msg': '申请成功'}
        return data
Example #3
0
    def post(self, **kwargs):

        # add operating log
        cur_time = datetime.datetime.utcnow()
        opName = 'CreateUser'
        opObject = "User"
        operator = "admin"
        opDetails = {}
        op_log_handler = OperatorLogHandler()

        args = user_create_parser.parse_args()
        username, password = args["username"], args["password"]
        opDetails['username'] = username
        role, active = args["role"], args["active"]
        balance = args["balance"]
        active = active == "true"
        salt = app.config.get("SALT", b"")
        password = bcrypt.hashpw(password.encode('utf8'), bytes(salt.encode()))
        status = "OK"
        user_id = ""

        try:
            user = User(username,
                        password,
                        is_admin=role == ADMIN,
                        role=role,
                        active=active,
                        balance=balance)
            user.save()
            user_id = user.id

            op_log_handler.record_operating_log(opDate=cur_time,
                                                opName=opName,
                                                opObject=opObject,
                                                resCode=200,
                                                operator=operator,
                                                opDetails=opDetails)

        except Exception as exc:
            logger.error("exc %s", exc)
            error_msg = "Fail to create user"
            status = "FAIL"
            op_log_handler.record_operating_log(opDate=cur_time,
                                                opName=opName,
                                                opObject=opObject,
                                                resCode=500,
                                                operator=operator,
                                                errorMsg=error_msg,
                                                opDetails=opDetails)

        return {"status": status, "id": user_id}, 200
Example #4
0
    def post(self, **kwargs):
        args = register_parser.parse_args()
        username, password = args["username"], args["password"]
        salt = app.config.get("SALT", b"")
        password = bcrypt.hashpw(password.encode('utf8'), bytes(salt.encode()))

        try:
            user = User(username, password)
            user_id = user.save()
            user = user.get_by_id(user_id)
            data = {
                "username": user.username,
                "apikey": str(user.id),
                "isActivated": user.active,
                "balance": user.balance,
                "success": True
            }
            return data, 200
        except Exception as exc:
            logger.error("exc %s", exc)
            data = {"success": False, "error": "register failed"}
            return data, 400