Example #1
0
    def put(self):
        """Update a user"""
        try:
            user_data = request.json
            existing_user = UserDao.get_by_id(user_data['id'])

            if 'firstname' in user_data:
                existing_user.firstname = user_data['firstname']

            if 'lastname' in user_data:
                existing_user.lastname = user_data['lastname']

            if 'password' in user_data:
                existing_user.set_password(user_data['password'])

            if 'status' in user_data:
                existing_user.status = user_data['status']

            existing_user = UserDao.update_user(existing_user)
            result = {
                'status': 'success',
                'message': 'User successfully updated.'
            }
            response_object = {
                'user': existing_user,
                'result': result,
            }
            return response_object, 200
        except Exception as e:
            result = {'status': 'error', 'message': 'Internal Server Error'}
            response_object = {
                'user': None,
                'result': result,
            }
            return response_object, 500
Example #2
0
 def post(self):
     # get the post data
     post_data = request.json
     try:
         user = UserDao.get_by_email(post_data['email'])
         if user and user.check_password(post_data['password']):
             auth_token = TokenUtil.encode_token(user.id)
             if auth_token:
                 authdata = {
                     'firstname': user.firstname,
                     'lastname': user.lastname,
                     'email': user.email,
                     'is_admin': 0,
                     'id': user.id,
                     'create_date': user.create_date,
                     'updated_date': user.updated_date,
                     'last_login_date': user.last_login_date,
                     'token': auth_token.decode()
                 }
                 result = {
                     'status': 'success',
                     'message': 'Successful user login.'
                 } 
                 response_object = {
                     'authdata': authdata,
                     'result': result,
                 }                  
                 UserDao.set_last_login_date(user.id)
                 return response_object, 200
         else:
             result = {
                 'status': 'fail',
                 'message': 'Email or password does not match.'
             }
             response_object = {
                 'authdata': None,
                 'result': result
             }
             return response_object, 401   
     except ApplicationException as e:
         result = {
             'status': 'fail',
             'message': 'Internal Server Error'
         }
         response_object = {
             'authdata': None,
             'result': result
         }
         return response_object, 500       
Example #3
0
def get_logged_in_user(new_request):
    # get the auth token
    auth_token = new_request.headers.get('Authorization')

    if auth_token:
        auth_token = auth_token.split(' ')
        if len(auth_token) < 2:
            response_object = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return response_object, 401
        resp = TokenUtil.decode_token(auth_token[1])
        if not isinstance(resp, str):
            user = UserDao.get_by_id(resp)
            response_object = {
                'status': 'success',
                'data': {
                    'id': user.id,
                    'email': user.email
                }
            }
            return response_object, 200
        response_object = {'status': 'fail', 'message': resp}
        return response_object, 401
    else:
        response_object = {
            'status': 'fail',
            'message': 'Provide a valid auth token.'
        }
        return response_object, 401
Example #4
0
    def post(self):
        """Insert a user"""
        try:
            user_data = request.json

            if UserDao.get_by_email(user_data['email']) is not None:
                response_object = {
                    'status': 'fail',
                    'message': 'User cannot be created.'
                }
                return response_object, 409

            if UserDao.get_by_email(user_data['email']) is not None:
                response_object = {
                    'status': 'fail',
                    'message': 'User cannot be created.'
                }
                return response_object, 409

            new_user = UserModel()
            new_user.firstname = user_data['firstname']
            new_user.lastname = user_data['lastname']
            new_user.email = user_data['email']
            new_user.set_password(user_data['password'])
            new_user = UserDao.save_user(new_user)
            response_object = {
                'status': 'success',
                'message': 'User successfully created.'
            }
            return response_object, 201
        except Exception as e:
            response_object = {
                'status': 'error',
                'message': 'Internal Server Error'
            }
            return response_object, 500
Example #5
0
    def post(self):
        """Change password"""
        try:
            user_data = request.json

            user = UserDao.get_by_id(user_data['id'])

            if user is not None and user.check_password(user_data['password']):
                new_user = UserDao.change_password(user.id,
                                                   user_data['newpassword'])
                response_object = {
                    'status': 'success',
                    'message': 'Password changed.'
                }
            else:
                response_object = {
                    'status': 'fail',
                    'message': 'Password cannot be changed.'
                }
                return response_object, 401

            return response_object, 200
        except Exception as e:
            return {'status': 'error', 'message': 'Internal Server Error'}, 500
Example #6
0
    def get(self):
        """Get all users"""
        try:
            users = UserDao.get_all()
            user_ret_list = []
            for user in users:
                user.password = None
                user_ret_list.append(user.to_json())

            result = {'status': 'success', 'message': 'User list returned.'}
            response_object = {'userlist': user_ret_list, 'result': result}
            return response_object, 200
        except Exception as e:
            result = {'status': 'error', 'message': 'Internal Server Error'}
            response_object = {'userlist': None, 'result': result}
            return response_object, 500
Example #7
0
 def get(self, id):
     """Get a user given its identifier"""
     user = UserDao.get_by_id(id)
     if not user:
         result = {'status': 'fail', 'message': 'User not found.'}
         response_object = {
             'user': None,
             'result': result,
         }
         return response_object, 404
     else:
         result = {'status': 'success', 'message': 'User found.'}
         response_object = {
             'user': user,
             'result': result,
         }
         return response_object, 200