예제 #1
0
파일: user.py 프로젝트: stpierre/pulp
    def create_user(self, login, password=None, name=None, roles=None):
        """
        Creates a new Pulp user and adds it to specified to roles.

        @param login: login name / unique identifier for the user
        @type  login: str

        @param password: password for login credentials
        @type  password: str

        @param name: user's full name
        @type  name: str

        @param roles: list of roles user will belong to
        @type  notes: dict

        @raise DuplicateResource: if there is already a user with the requested login
        @raise InvalidValue: if any of the fields are unacceptable
        """

        existing_user = User.get_collection().find_one({'login' : login})
        if existing_user is not None:
            raise DuplicateResource(login)

        invalid_values = []

        if login is None or not is_user_login_valid(login):
            invalid_values.append('login')
        if name is not None and not isinstance(name, basestring):
            invalid_values.append('name')
        if roles is not None and not isinstance(roles, list):
            invalid_values.append('roles')

        if invalid_values:
            raise InvalidValue(invalid_values)

        # Use the login for user name if one was not specified
        name = name or login
        roles = roles or None

        # Encode plain-text password
        hashed_password = None
        if password:
            hashed_password = password_util.hash_password(password)

        # Creation
        create_me = User(login=login, password=hashed_password, name=name, roles=roles)
        User.get_collection().save(create_me, safe=True)

        # Grant permissions
        grant_automatic_permissions_for_new_user(create_me['login'])

        # Retrieve the user to return the SON object
        created = User.get_collection().find_one({'login' : login})

        return created
예제 #2
0
파일: user.py 프로젝트: stpierre/pulp
 def create(self, login, password=None, name=None, id=None):
     """
     Create a new User object and return it
     """
     if id is None:
         id = str(uuid.uuid4())
     hashed_password = None
     if (password is not None):
         hashed_password = password_util.hash_password(password)
     user = model.User(login, id, hashed_password, name)
     self.collection.insert(user, safe=True)
     return user
예제 #3
0
파일: user.py 프로젝트: stpierre/pulp
    def update_user(self, login, delta):
        """
        Updates the user. Following fields may be updated through this call:
        * password
        * name
        * roles

        Other fields found in delta will be ignored.

        @param login: identifies the user
        @type  login: str

        @param delta: list of attributes and their new values to change
        @type  delta: dict

        @raise MissingResource: if there is no user with login
        """

        user_coll = User.get_collection()

        user = user_coll.find_one({'login' : login})
        if user is None:
            raise MissingResource(login)

        # Check
        invalid_values = []
        if 'password' in delta:
            if not isinstance(delta['password'], basestring):
                invalid_values.append('password')
            else:
                user['password'] = password_util.hash_password(delta['password'])

        if 'name' in delta:
            if delta['name'] is not None and not isinstance(delta['name'], basestring):
                invalid_values.append('name')
            else:
                user['name'] = delta['name']

        if 'roles' in delta:
            if delta['roles'] is not None and not isinstance(delta['roles'], list):
                invalid_values.append('roles')
            else:
                user['roles'] = delta['roles']

        if invalid_values:
            raise InvalidValue(invalid_values)

        user_coll.save(user, safe=True)

        return user
예제 #4
0
파일: user.py 프로젝트: stpierre/pulp
 def update(self, login, delta):
     """
     Update a user and hash the inbound password if it is different
     from the existing password.
     @param login: The user login.
     @param delta: A dict of fields to change.
     """
     delta.pop('login', None)
     user = self.user(login)
     for key, value in delta.items():
         # simple changes
         if key in ('name', 'roles',):
             user[key] = value
             continue
         # password changed
         if key == 'password':
             if value:
                 user['password'] = password_util.hash_password(value)
             continue
         raise Exception, \
             'update keyword "%s", not-supported' % key
     self.collection.save(user, safe=True)
     return user
예제 #5
0
 def test_check_password(self):
     password = "******"
     hashed = hash_password(password)
     self.assertTrue(check_password(hashed, password))
예제 #6
0
 def test_hash_password(self):
     password = "******"
     hashed = hash_password(password)
     self.assertNotEqual(hashed, password)