Ejemplo n.º 1
0
    def new(self):
        """ Insert new user in the database """
        form = self.params
        self.__request_account()
        self.__request_profile()
        self.__request_password(new_user=True)

        if self.changed_email:
            self.user["email"] = self.user["new_email"]

        if self.message is None:
            self.user["password"] = create_password(form["password_new"])
            del (self.user["password_new"])
            del (self.user["password_check"])
            self.user["status"] = ACTIVATED

            if "image_tmp" in self.user:
                del (self.user["image_tmp"])

            self.user["_id"] = model.users.create(self.user)
            if len(form.get("image_uploaded", "")) > 0:
                if self.__upload_avatar():
                    self.user["image"] = self.list_images
                    model.users.update(user_id=self.user["_id"], user=self.user)

            self.success = True
            self.message = g.users_msg("success_new_user")

        return False
Ejemplo n.º 2
0
    def new(self):
        """ Insert new user in the database """
        form = self.params
        self.__request_account()
        self.__request_profile()
        self.__request_password(new_user=True)

        if self.changed_email:
            self.user['email'] = self.user['new_email']

        if self.message is None:
            self.user['password'] = create_password(form['password_new'])
            del (self.user['password_new'])
            del (self.user['password_check'])
            self.user['status'] = ACTIVATED

            if 'image_tmp' in self.user:
                del (self.user['image_tmp'])

            self.user['_id'] = model.users.create(self.user)
            if len(form.get('image_uploaded', '')) > 0:
                if self.__upload_avatar():
                    self.user['image'] = self.list_images
                    model.users.update(user_id=self.user['_id'],
                                       user=self.user)

            self.success = True
            self.message = g.users_msg('success_new_user')

        return False
Ejemplo n.º 3
0
    def __request_password(self, new_user=False, old_password=False):
        """ Get from request.form the password values and check it """
        form = self.params
        password = form.get("password")
        password_new = form.get("password_new")
        password_check = form.get("password_check")

        if self.message:
            return False

        # Check that the password_new field is not empty
        if new_user and (password_new is None or len(password_new) == 0):
            self.message = g.users_msg("error_password_0")

        # Check that the password_check field is not empty
        elif new_user and (password_check is None or len(password_check) == 0):
            self.message = g.users_msg("error_password_2")

        elif password_new and len(password_new):
            self.user["password_new"] = password_new
            self.user["password_check"] = password_check

            # Check that the new password has between 6 and 30 characters.
            if not check.length(self.user["password_new"], 6, 30):
                self.message = g.users_msg("error_password_1")

            # Check that both passwords are the same
            elif self.user["password_new"] != self.user["password_check"]:
                self.message = g.users_msg("error_password_2")

        if old_password:
            # Verify that the old password matches the one entered.
            old_password = create_password(password)
            if self.user["password"] != old_password:
                self.message = g.users_msg("error_password_3")
Ejemplo n.º 4
0
    def update(self):
        """ Update user values in the database """
        form = self.params
        self.__request_account()
        self.__request_profile()
        self.__request_password()

        if self.changed_email:
            self.user["email"] = self.user["new_email"]

        if len(form.get("image_uploaded", "")) > 0:
            if self.__upload_avatar():
                self.user["image"] = self.list_images

        if self.message is None:
            if len(form["password_new"]):
                self.user["password"] = create_password(form["password_new"])
                del (self.user["password_new"])
                del (self.user["password_check"])

            if "image_tmp" in self.user:
                del (self.user["image_tmp"])

            model.users.update(user_id=self.user["_id"], user=self.user)
            self.success = True
            self.message = g.users_msg("success_update_user")

        self.user["password_new"] = ""
        self.user["password_check"] = ""
Ejemplo n.º 5
0
    def __request_password(self, new_user=False, old_password=False):
        """ Get from request.form the password values and check it """
        form = self.params
        password = form.get('password')
        password_new = form.get('password_new')
        password_check = form.get('password_check')

        if self.message:
            return False

        # Check that the password_new field is not empty
        if new_user and (password_new is None or len(password_new) == 0):
            self.message = g.users_msg('error_password_0')

        # Check that the password_check field is not empty
        elif new_user and (password_check is None or len(password_check) == 0):
            self.message = g.users_msg('error_password_2')

        elif password_new and len(password_new):
            self.user['password_new'] = password_new
            self.user['password_check'] = password_check

            # Check that the new password has between 6 and 30 characters.
            if not check.length(self.user['password_new'], 6, 30):
                self.message = g.users_msg('error_password_1')

            # Check that both passwords are the same
            elif self.user['password_new'] != self.user['password_check']:
                self.message = g.users_msg('error_password_2')

        if old_password:
            # Verify that the old password matches the one entered.
            old_password = create_password(password)
            if self.user['password'] != old_password:
                self.message = g.users_msg('error_password_3')
Ejemplo n.º 6
0
    def update(self):
        """ Update user values in the database """
        form = self.params
        self.__request_account()
        self.__request_profile()
        self.__request_password()

        if self.changed_email:
            self.user['email'] = self.user['new_email']

        if len(form.get('image_uploaded', '')) > 0:
            if self.__upload_avatar():
                self.user['image'] = self.list_images

        if self.message is None:
            if len(form['password_new']):
                self.user['password'] = create_password(form['password_new'])
                del (self.user['password_new'])
                del (self.user['password_check'])

            if 'image_tmp' in self.user:
                del (self.user['image_tmp'])

            model.users.update(user_id=self.user['_id'], user=self.user)
            self.success = True
            self.message = g.users_msg('success_update_user')

        self.user['password_new'] = ""
        self.user['password_check'] = ""
Ejemplo n.º 7
0
    def update_password(self):
        """ Update user values in the database """
        form = self.params
        old_password = self.user.get("password", False)
        self.__request_password(old_password=old_password)
        if self.message is None:
            self.user["password"] = create_password(form["password_new"])
            del (self.user["password_new"])
            del (self.user["password_check"])

            model.users.update(user_id=self.user["_id"], user=self.user)
            self.success = True
            self.message = g.users_msg("success_update_password")

        self.user["password"] = ""
        self.user["password_new"] = ""
        self.user["password_check"] = ""
Ejemplo n.º 8
0
    def update_password(self):
        """ Update user values in the database """
        form = self.params
        old_password = self.user.get('password', False)
        self.__request_password(old_password=old_password)
        if self.message is None:
            self.user['password'] = create_password(form['password_new'])
            del (self.user['password_new'])
            del (self.user['password_check'])

            model.users.update(user_id=self.user['_id'], user=self.user)
            self.success = True
            self.message = g.users_msg('success_update_password')

        self.user['password'] = ""
        self.user['password_new'] = ""
        self.user['password_check'] = ""
Ejemplo n.º 9
0
def sign_in(username_or_email=None, password=None, permanent=None):
    """
    Sign the user in.
    We check the user by both the username or email. 

    """
    error_code = None

    if not username_or_email or not password:
        error_code = ('login_msg', 'login_error_1')

    if not error_code:
        user = model.users.find(username=username_or_email,
                                only_one=True,
                                my_rank=10)

        if user is None:
            user = model.users.find(email=username_or_email,
                                    only_one=True,
                                    my_rank=10)

        if user is None:
            error_code = ('login_msg', 'login_error_2')

        if not error_code and user["status"] == NOTACTIVATED:
            error_code = ('login_msg', 'login_error_3')

        elif not error_code and not user['password'] == create_password(
                password):
            error_code = ('login_msg', 'login_error_2')

        if not error_code:
            model.users.update(user_id=user["_id"])
            if permanent is not None:
                permanent = True
            return {
                "success": True,
                "user_id": str(user['_id']),
                "permanent": permanent
            }
    return dict(success=False, errors=[{"code": error_code}])
Ejemplo n.º 10
0
def sign_in(username_or_email=None,
            password=None,
            permanent=None):
    """
    Sign the user in.
    We check the user by both the username or email. 

    """
    error_code = None

    if not username_or_email or not password:
        error_code = ('login_msg', 'login_error_1')

    if not error_code:
        user = model.users.find(username=username_or_email, only_one=True, my_rank=10)

        if user is None:
            user = model.users.find(email=username_or_email, only_one=True, my_rank=10)

        if user is None:
            error_code = ('login_msg', 'login_error_2')

        if not error_code and user["status"] == NOTACTIVATED:
            error_code = ('login_msg', 'login_error_3')

        elif not error_code and not user['password'] == create_password(password):
            error_code = ('login_msg', 'login_error_2')

        if not error_code:
            model.users.update(user_id=user["_id"])
            if permanent is not None:
                permanent = True
            return {
                "success": True,
                "user_id": str(user['_id']),
                "permanent": permanent
            }
    return dict(success=False, errors=[{ "code": error_code }])