Ejemplo n.º 1
0
    def get(self, user_id):
        """GET User

        Returns all information associated to the user of user_id. This route
        requires an active login and is limited to the user himself and all
        user of role permission defined in user_route decorator. Defaults to
        users of role 'superuser' or 'admin'

        Parameters
        ----------
        user_id : str
            id of the requested user

        Returns
        -------
        user : JSON
            JSON serialized content of the User information.

        """
        # get User
        user = User.get(_id=user_id)

        # check user exists
        if user is None:
            return {'status': 404, 'message': 'User not found'}, 405

        return user.to_dict(stringify=True), 200
Ejemplo n.º 2
0
    def put(self, user_id):
        """Request new activation link

        A PUT request to request a new activation link. This route will
        overwrite old activation tokens and needs a email client configured.

        Parameters
        ----------
        user_id : str
            id of the requested user

        Returns
        -------
        response : JSON
            JSON serialized acknowledgement message

        """
        # load the user
        user = User.get(_id=user_id)

        if user is None:
            return {'status': 404, 'message': 'User not found.'}, 404

        # create a new activation token
        token = user.get_activation_token()

        # build the activation link
        url = url_for('auth.activation',
                      token=token,
                      user_id=str(user.id),
                      _external=True)

        # build the activation mail and sent
        #        msg = Message(
        #            'Your account activation',
        #            recipients=[user.email],
        #            html=ACTIVATION_MAIL_TEMPLATE.format(user.email, url)
        #        )
        mail = Mail()
        mail.send(user.email, 'Your new activation link',
                  ACTIVATION_MAIL_TEMPLATE.format(user.email, url))

        return {
            'status':
            200,
            'message':
            'A new activation mail has been sent to: %s.' +
            'Your user ID is: %s' % (user.email, str(user.id))
        }, 200
Ejemplo n.º 3
0
    def delete(self, user_id):
        """Delete the user

        Delete the requested user. This action can only be performed by the
        user of user_id himself or any user of role superuser and users of
        decorated roles (usually 'admin').

        The user information will be stored into the backup folder into the
        json backup file specified in the application cofig

        Parameters
        ----------
        user_id : str
            id of the requested user

        Returns
        -------
        response : JSON
            acknowledgement message

        """
        # get the user
        user = User.get(_id=user_id)

        if user is None:
            return {'status': 404, 'message': 'User not found.'}, 404

        # get the user info
        d = user.to_dict(stringify=True)

        # delete
        user.delete()

        # store
        with open(current_app.config.get('DELETED_USER_PATH'), 'w+') as backup:
            data = json.load(backup)
            data['users'].append(d)
            backup.write(data)

        # return
        return {
            'status': 200,
            'acknowledged': True,
            'message': 'User has been deleted.'
        }, 200
Ejemplo n.º 4
0
    def get(self, user_id):
        """Link Activation

        This route is used to activate a user account by a GET request. Then
        the activation token has to be passed by url PARAM like:
        SERVER/user/user_id/activate?token=YOURTOKEN

        Parameters
        ----------
        user_id : str
            id of the requested user

        Returns
        -------
        user : JSON
            JSON serialized content of the activated User information.

        """
        # make sure a token was passed
        token = request.args.get('token')
        if token is None:
            return {
                'status': 409,
                'message': 'No activation token was passed.'
            }, 409

        # load the user
        user = User.get(_id=user_id)

        if user is None:
            return {'status': 404, 'message': 'User not found.'}, 404

        # activate
        if user.activate(token=token):
            return {
                'status': 200,
                'acknowledged': True,
                'user': user.to_dict(stringify=True)
            }, 200
        else:
            return {
                'status': 400,
                'message': 'The activation token is invalid.'
            }, 400
Ejemplo n.º 5
0
    def post(self, user_id):
        """Edit user

        Request an edit to the user of user_id. Any JSON encoded content
        passed to this route will update the user of user_id.

        Parameters
        ----------
        user_id : str
            id of the requested user

        Returns
        -------
        user : JSON
            JSON serialized content of the updated User information.

        """
        # get User
        user = User.get(_id=user_id)

        # check user exists
        if user is None:
            return {'status': 404, 'message': 'User not found'}, 405

        # get the data
        data = request.get_json()
        if len(data.keys()) == 0:
            return {'status': 412, 'message': 'No content recieved'}, 412

        # update the user
        try:
            user.update(data=data)
        except Exception as e:
            return {'status': 500, 'message': str(e)}, 500

        # return updated user
        return user.to_dict(stringify=True), 200