Changeset - 92da990f9eaf
[Not reviewed]
default
0 1 0
Marcin Kuzminski - 12 years ago 2013-07-01 16:14:46
marcin@python-works.com
Removed redundant Exception catching
1 file changed with 25 insertions and 23 deletions:
0 comments (0 inline, 0 general)
rhodecode/model/user.py
Show inline comments
 
@@ -77,31 +77,31 @@ class UserModel(BaseModel):
 
    def get_by_email(self, email, cache=False, case_insensitive=False):
 
        return User.get_by_email(email, case_insensitive, cache)
 

	
 
    def get_by_api_key(self, api_key, cache=False):
 
        return User.get_by_api_key(api_key, cache)
 

	
 
    def create(self, form_data, cur_user=None):
 
        if not cur_user:
 
            cur_user = getattr(get_current_rhodecode_user(), 'username', None)
 

	
 
        from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
 
        _fd = form_data
 
        form_data = {
 
        user_data = {
 
            'username': _fd['username'], 'password': _fd['password'],
 
            'email': _fd['email'], 'firstname': _fd['firstname'], 'lastname': _fd['lastname'],
 
            'active': _fd['active'], 'admin': False
 
        }
 
        # raises UserCreationError if it's not allowed
 
        check_allowed_create_user(form_data, cur_user)
 
        check_allowed_create_user(user_data, cur_user)
 

	
 
        from rhodecode.lib.auth import get_crypt_password
 
        try:
 
            new_user = User()
 
            for k, v in form_data.items():
 
                if k == 'password':
 
                    v = get_crypt_password(v)
 
                if k == 'firstname':
 
                    k = 'name'
 
                setattr(new_user, k, v)
 

	
 
            new_user.api_key = generate_api_key(form_data['username'])
 
@@ -126,31 +126,31 @@ class UserModel(BaseModel):
 
        :param firstname:
 
        :param lastname:
 
        :param active:
 
        :param admin:
 
        :param ldap_dn:
 
        :param cur_user:
 
        """
 
        if not cur_user:
 
            cur_user = getattr(get_current_rhodecode_user(), 'username', None)
 

	
 
        from rhodecode.lib.auth import get_crypt_password
 
        from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
 
        form_data = {
 
        user_data = {
 
            'username': username, 'password': password,
 
            'email': email, 'firstname': firstname, 'lastname': lastname,
 
            'active': active, 'admin': admin
 
        }
 
        # raises UserCreationError if it's not allowed
 
        check_allowed_create_user(form_data, cur_user)
 
        check_allowed_create_user(user_data, cur_user)
 

	
 
        log.debug('Checking for %s account in RhodeCode database' % username)
 
        user = User.get_by_username(username, case_insensitive=True)
 
        if user is None:
 
            log.debug('creating new user %s' % username)
 
            new_user = User()
 
            edit = False
 
        else:
 
            log.debug('updating user %s' % username)
 
            new_user = user
 
            edit = True
 

	
 
@@ -185,31 +185,31 @@ class UserModel(BaseModel):
 
        """
 
        if not cur_user:
 
            cur_user = getattr(get_current_rhodecode_user(), 'username', None)
 
        if self.get_by_username(username, case_insensitive=True) is None:
 
            # autogenerate email for container account without one
 
            generate_email = lambda usr: '%s@container_auth.account' % usr
 
            firstname = attrs['name']
 
            lastname = attrs['lastname']
 
            active = attrs.get('active', True)
 
            email = attrs['email'] or generate_email(username)
 

	
 
            from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
 
            form_data = {
 
            user_data = {
 
                'username': username, 'password': None,
 
                'email': email, 'firstname': firstname, 'lastname': lastname,
 
                'active': attrs.get('active', True), 'admin': False
 
            }
 
            # raises UserCreationError if it's not allowed
 
            check_allowed_create_user(form_data, cur_user)
 
            check_allowed_create_user(user_data, cur_user)
 

	
 
            try:
 
                new_user = User()
 
                new_user.username = username
 
                new_user.password = None
 
                new_user.api_key = generate_api_key(username)
 
                new_user.email = email
 
                new_user.active = active
 
                new_user.name = firstname
 
                new_user.lastname = lastname
 

	
 
                self.sa.add(new_user)
 
@@ -239,31 +239,31 @@ class UserModel(BaseModel):
 
        from rhodecode.lib.auth import get_crypt_password
 
        log.debug('Checking for such ldap account in RhodeCode database')
 
        if self.get_by_username(username, case_insensitive=True) is None:
 
            # autogenerate email for container account without one
 
            generate_email = lambda usr: '%s@ldap.account' % usr
 
            password = get_crypt_password(password)
 
            firstname = attrs['name']
 
            lastname = attrs['lastname']
 
            active = attrs.get('active', True)
 
            email = attrs['email'] or generate_email(username)
 

	
 
            from rhodecode.lib.hooks import log_create_user, check_allowed_create_user
 
            form_data = {
 
            user_data = {
 
                'username': username, 'password': password,
 
                'email': email, 'firstname': firstname, 'lastname': lastname,
 
                'active': attrs.get('active', True), 'admin': False
 
            }
 
            # raises UserCreationError if it's not allowed
 
            check_allowed_create_user(form_data, cur_user)
 
            check_allowed_create_user(user_data, cur_user)
 

	
 
            try:
 
                new_user = User()
 
                username = username.lower()
 
                # add ldap account always lowercase
 
                new_user.username = username
 
                new_user.password = password
 
                new_user.api_key = generate_api_key(username)
 
                new_user.email = email
 
                new_user.active = active
 
                new_user.ldap_dn = safe_unicode(user_dn)
 
                new_user.name = firstname
 
@@ -402,49 +402,51 @@ class UserModel(BaseModel):
 
            else:
 
                log.debug("password reset email %s not found" % user_email)
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            return False
 

	
 
        return True
 

	
 
    def reset_password(self, data):
 
        from rhodecode.lib.celerylib import tasks, run_task
 
        from rhodecode.lib import auth
 
        user_email = data['email']
 
        pre_db = True
 
        try:
 
            try:
 
                user = User.get_by_email(user_email)
 
                new_passwd = auth.PasswordGenerator().gen_password(8,
 
                                 auth.PasswordGenerator.ALPHABETS_BIG_SMALL)
 
                if user:
 
                    user.password = auth.get_crypt_password(new_passwd)
 
                    user.api_key = auth.generate_api_key(user.username)
 
                    Session().add(user)
 
                    Session().commit()
 
                    log.info('change password for %s' % user_email)
 
                if new_passwd is None:
 
                    raise Exception('unable to generate new password')
 
            except Exception:
 
                log.error(traceback.format_exc())
 
                Session().rollback()
 
            user = User.get_by_email(user_email)
 
            new_passwd = auth.PasswordGenerator().gen_password(8,
 
                            auth.PasswordGenerator.ALPHABETS_BIG_SMALL)
 
            if user:
 
                user.password = auth.get_crypt_password(new_passwd)
 
                user.api_key = auth.generate_api_key(user.username)
 
                Session().add(user)
 
                Session().commit()
 
                log.info('change password for %s' % user_email)
 
            if new_passwd is None:
 
                raise Exception('unable to generate new password')
 

	
 
            pre_db = False
 
            run_task(tasks.send_email, user_email,
 
                     _('Your new password'),
 
                     _('Your new RhodeCode password:%s') % (new_passwd,))
 
            log.info('send new password mail to %s' % user_email)
 

	
 
        except Exception:
 
            log.error('Failed to update user password')
 
            log.error(traceback.format_exc())
 
            if pre_db:
 
                # we rollback only if local db stuff fails. If it goes into
 
                # run_task, we're pass rollback state this wouldn't work then
 
                Session().rollback()
 

	
 
        return True
 

	
 
    def fill_data(self, auth_user, user_id=None, api_key=None):
 
        """
 
        Fetches auth_user by user_id,or api_key if present.
 
        Fills auth_user attributes with those taken from database.
 
        Additionally set's is_authenitated if lookup fails
 
        present in database
 

	
 
        :param auth_user: instance of user to set attributes
 
        :param user_id: user id to fetch by
0 comments (0 inline, 0 general)