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
 
@@ -83,19 +83,19 @@ class UserModel(BaseModel):
 
    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':
 
@@ -132,19 +132,19 @@ class UserModel(BaseModel):
 
        """
 
        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()
 
@@ -191,19 +191,19 @@ class UserModel(BaseModel):
 
            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)
 
@@ -245,19 +245,19 @@ class UserModel(BaseModel):
 
            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
 
@@ -408,37 +408,39 @@ class UserModel(BaseModel):
 
        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.
0 comments (0 inline, 0 general)