Example #1
0
    def post(self):
        """Creates new user account if provided valid arguments"""
        parser = reqparse.RequestParser()
        parser.add_argument('email', type=UserValidator.create('unique_email'), required=True)
        parser.add_argument('username', type=UserValidator.create('unique_username'))
        parser.add_argument('password', type=UserValidator.create('password'))
        parser.add_argument('remember', type=inputs.boolean, default=False)
        args = parser.parse_args()

        user_db = auth.create_user_db(
            auth_id=None,
            name='',
            username=args.username,
            email=args.email,
            verified=True if not config.CONFIG_DB.verify_email else False,
            password=args.password
        )
        user_db.put()

        if config.CONFIG_DB.verify_email:
            task.verify_user_email_notification(user_db)
            return make_empty_ok_response()

        # if users don't need to verify email, we automaticaly signin newly registered user
        auth.signin_user_db(user_db, remember=args.remember)
        return user_db.to_dict(include=User.get_private_properties())
Example #2
0
    def post(self):
        """Creates new user account if provided valid arguments"""
        parser = reqparse.RequestParser()
        parser.add_argument('email', type=UserValidator.create('unique_email'), required=True)
        parser.add_argument('username', type=UserValidator.create('unique_username'))
        parser.add_argument('password', type=UserValidator.create('password'))
        parser.add_argument('remember', type=inputs.boolean, default=False)
        args = parser.parse_args()

        user_db = auth.create_user_db(
            auth_id=None,
            name='',
            username=args.username,
            email=args.email,
            verified=True if not config.CONFIG_DB.verify_email else False,
            password=args.password
        )
        user_db.put()

        if config.CONFIG_DB.verify_email:
            task.verify_user_email_notification(user_db)
            return make_empty_ok_response()

        # if users don't need to verify email, we automaticaly signin newly registered user
        auth.signin_user_db(user_db, remember=args.remember)
        return user_db.to_dict(include=User.get_private_properties())
Example #3
0
    def post(self):
        """Creates new user account if provided valid arguments"""
        parser = reqparse.RequestParser()
        parser.add_argument('first_name',
                            type=UserValidator.create('name'),
                            required=True)
        parser.add_argument('last_name',
                            type=UserValidator.create('name'),
                            required=True)
        parser.add_argument('email',
                            type=UserValidator.create('unique_email'),
                            required=True)
        parser.add_argument('password',
                            type=UserValidator.create('password'),
                            required=True)
        parser.add_argument('terms',
                            type=bool,
                            required=True,
                            help='Must agree to all terms and conditions')
        args = parser.parse_args()

        if not args.terms:
            return ApiException.error(107)

        count = 0
        username = util.create_username_from_email(args.email)
        while (True):  # get a unique username
            if User.is_username_available(username):
                break
            username += str(count)
            count += 1

        user_db = auth.create_user_db(
            auth_id=None,
            username=util.create_username_from_email(args.email),
            email=args.email,
            verified=True if not config.CONFIG_DB.verify_email else False,
            password=args.password,
            avatar_url=User.get_gravatar_url(args.email),
            roles=[User.Roles.MEMBER],
            first_name=args.first_name,
            last_name=args.last_name,
        )
        user_db.put()

        Profile.get_or_create(user_db)

        if config.CONFIG_DB.verify_email:
            task.verify_user_email_notification(user_db)

        # sign in user
        auth.signin_user_db(user_db, remember=True)
        return user_db.to_dict(include=User.get_private_properties())
Example #4
0
 def post(self, key):
     """Changes user's password"""
     parser = reqparse.RequestParser()
     parser.add_argument('currentPassword', type=UserValidator.create('password', required=False), dest='current_password')
     parser.add_argument('newPassword', type=UserValidator.create('password'), dest='new_password')
     args = parser.parse_args()
     # Users, who signed up via social networks have empty password_hash, so they have to be allowed
     # to change it as well
     if g.model_db.password_hash != '' and not g.model_db.has_password(args.current_password):
         raise ValueError('Given password is incorrect.')
     g.model_db.password_hash = util.password_hash(args.new_password)
     g.model_db.put()
     return make_empty_ok_response()
Example #5
0
 def post(self):
     """Sets new password given by user if he provided valid token
     Notice ndb.toplevel decorator here, so we can perform asynchronous put
      and signing in in parallel
     """
     parser = reqparse.RequestParser()
     parser.add_argument('token', type=UserValidator.create('token'))
     parser.add_argument('newPassword', type=UserValidator.create('password'), dest='new_password')
     args = parser.parse_args()
     user_db = User.get_by('token', args.token)
     user_db.password_hash = util.password_hash(args.new_password)
     user_db.token = util.uuid()
     user_db.verified = True
     user_db.put_async()
     auth.signin_user_db(user_db)
     return user_db.to_dict(include=User.get_private_properties())
Example #6
0
 def post(self):
     """Sets new password given by user if he provided valid token
     Notice ndb.toplevel decorator here, so we can perform asynchronous put
      and signing in in parallel
     """
     parser = reqparse.RequestParser()
     parser.add_argument('token', type=UserValidator.create('token'))
     parser.add_argument('newPassword', type=UserValidator.create('password'), dest='new_password')
     args = parser.parse_args()
     user_db = User.get_by('token', args.token)
     user_db.password_hash = util.password_hash(args.new_password)
     user_db.token = util.uuid()
     user_db.verified = True
     user_db.put_async()
     auth.signin_user_db(user_db)
     return user_db.to_dict(include=User.get_private_properties())
Example #7
0
 def post(self):
     """Sends email with token for resetting password to an user"""
     parser = reqparse.RequestParser()
     parser.add_argument('email', type=UserValidator.create('existing_email'))
     args = parser.parse_args()
     user_db = User.get_by('email', args.email)
     task.reset_password_notification(user_db)
     return make_empty_ok_response()
Example #8
0
 def post(self):
     """Sends email with token for resetting password to an user"""
     parser = reqparse.RequestParser()
     parser.add_argument('email', type=UserValidator.create('existing_email'))
     args = parser.parse_args()
     user_db = User.get_by('email', args.email)
     task.reset_password_notification(user_db)
     return make_empty_ok_response()
Example #9
0
 def post(self, key):
     """Changes user's password"""
     parser = reqparse.RequestParser()
     parser.add_argument('currentPassword',
                         type=UserValidator.create('password',
                                                   required=False),
                         dest='current_password')
     parser.add_argument('newPassword',
                         type=UserValidator.create('password'),
                         dest='new_password')
     args = parser.parse_args()
     # Users, who signed up via social networks have empty password_hash, so they have to be allowed
     # to change it as well
     if g.model_db.password_hash != '' and not g.model_db.has_password(
             args.current_password):
         raise ValueError('Given password is incorrect.')
     g.model_db.password_hash = util.password_hash(args.new_password)
     g.model_db.put()
     return make_empty_ok_response()
Example #10
0
 def post(self):
     """Sends feedback email to admin"""
     if not config.CONFIG_DB.feedback_email:
         return abort(418)
     parser = reqparse.RequestParser()
     parser.add_argument('message', type=ArgumentValidator.create('feedback'), required=True)
     parser.add_argument('email', type=UserValidator.create('email', required=False))
     args = parser.parse_args()
     body = '%s\n\n%s' % (args.message, args.email)
     kwargs = {'reply_to': args.email} if args.email else {}
     task.send_mail_notification('%s...' % body[:48].strip(), body, **kwargs)
     return make_empty_ok_response()
Example #11
0
    def post(self, key):
        """Updates user's properties"""
        update_properties = [
            'first_name', 'last_name', 'avatar_url', 'email', 'username'
        ]
        if auth.is_admin():
            update_properties += ['verified', 'active', 'admin']
        new_user_data = _.pick(request.json, update_properties)

        new_email_set = False
        new_email = new_user_data.get('email')
        if new_email != g.model_db.email:
            UserValidator.create('unique_email')(new_email)
            new_email_set = True
        new_username = new_user_data.get('username')
        if new_username != g.model_db.username:
            UserValidator.create('unique_username')(new_username)

        g.model_db.populate(**new_user_data)
        g.model_db.put()
        return g.model_db.to_dict(include=User.get_public_properties())
Example #12
0
 def post(self):
     """Sends feedback email to admin"""
     if not config.CONFIG_DB.feedback_email:
         return abort(418)
     parser = reqparse.RequestParser()
     parser.add_argument('message', type=ArgumentValidator.create('feedback'), required=True)
     parser.add_argument('email', type=UserValidator.create('email', required=False))
     args = parser.parse_args()
     body = '%s\n\n%s' % (args.message, args.email)
     kwargs = {'reply_to': args.email} if args.email else {}
     task.send_mail_notification('%s...' % body[:48].strip(), body, **kwargs)
     return make_empty_ok_response()
Example #13
0
def inject_validators():
    """Injects 'validators' variable into jinja template, so it can be passed into angular. See base.html
    Model validators are passed to angular so it can be used for frontend input validation as well
    This prevents code repetition, as we e.g we change property of UserValidator.name to [5, 20]
    and the same validation of user's name (length between 5-20 characters) will be performed in frontend
    as well as in backend
    """
    return {
        'validators': {
            'arg': ArgumentValidator.to_dict(),
            'user': UserValidator.to_dict()
        }
    }
Example #14
0
def inject_validators():
    """Injects 'validators' variable into jinja template, so it can be passed into angular. See base.html
    Model validators are passed to angular so it can be used for frontend input validation as well
    This prevents code repetition, as we e.g we change property of UserValidator.name to [5, 20]
    and the same validation of user's name (length between 5-20 characters) will be performed in frontend
    as well as in backend
    """
    return {
        'validators': {
            'arg': ArgumentValidator.to_dict(),
            'user': UserValidator.to_dict()
        }
    }