Esempio n. 1
0
 def post(self):
     data = User.parser.parse_args()
     user = UserModel.find_by_username(data['user_name'])
     if user:
         raise HandleException("User already exists", status_code=409)
     user = UserModel(**data)
     print(user)
     user.save_to_db()
     return {
         "message":
         "user with name {} {} add successfully".format(
             data['first_name'], data['last_name'])
     }
Esempio n. 2
0
    def update(self):
        data = User.parser.parse_args()
        user = UserModel.find_by_username(data['username'])

        if user is None:
            user = UserModel(**data)
        else:
            user.first_name = data['first_name']
            user.last_name = data['last_name']
            user.user_name = data['user_name']
            user.role = data['role']

        user.save_to_db()
        return user.json()
Esempio n. 3
0
 def get(self, username):
     user = UserModel.find_by_username(username)
     if not user:
         return {
             "message":
             "could not find user with '{}' username".format(username)
         }, 404
     return user.json()
Esempio n. 4
0
def authenticate(user_name, password):
    print('--------')
    user = UserModel.find_by_username(user_name)
    if user and safe_str_cmp(user.password, password):
        user.id = user.user_id
        user.role = "genadi"
        print(user.user_name)
        print(user.id)
        print(user.role)
        return user
Esempio n. 5
0
 def post(self):
     user_name = request.json.get('username')
     password = request.json.get('password')
     user = UserModel.find_by_username(user_name)
     if not user or not bcrypt.check_password_hash(user.password, password):
         return {"message": "Bad username or password"}, 401
     user_object = {"user_name": user.user_name, "roles": user.role_id}
     access_token = create_access_token(identity=user_object)
     ret = {'access_token': access_token}
     return ret, 200
Esempio n. 6
0
    def delete(self):
        data = User.parser.parse_args()
        user = UserModel.find_by_username(data['username'])

        if user:
            user.delete_from_db()
            return {"message": "delete successfully"}

        return {
            "message":
            "could not find user with '{}' username".format(data['username'])
        }, 404
Esempio n. 7
0
def identity(payload):
    print('++++++++')
    user_id = payload['identity']
    return UserModel.find_by_user_id(user_id)