def get_user_by_name(self, user_name, domain_id): with sql.session_for_read() as session: query = session.query(model.User).join(model.LocalUser) query = query.filter( sqlalchemy.and_(model.LocalUser.name == user_name, model.LocalUser.domain_id == domain_id)) try: user_ref = query.one() except sql.NotFound: raise exception.UserNotFound(user_id=user_name) return base.filter_user(user_ref.to_dict())
def _assert_valid_user_id(self, user_id): """Ensure a valid user id. :param context: standard context :param user_id: expected credential owner :raises exception.UserNotFound: on failure """ user_ref = self.identity_api.get_user(user_id) if not user_ref: raise exception.UserNotFound(user_id=user_id)
def wrapper(self, *args, **kwargs): try: return f(self, *args, **kwargs) except exception.PublicIDNotFound as e: if exception_type == 'user': raise exception.UserNotFound(user_id=str(e)) elif exception_type == 'group': raise exception.GroupNotFound(group_id=str(e)) elif exception_type == 'assertion': raise AssertionError(_('Invalid user / password')) else: raise
def update(self, role_id, role): if role['id'] != role_id: raise exception.ValidationError('Cannot change role ID') try: old_name = self.get_by_name(role['name']) raise exception.Conflict('Cannot duplicate name %s' % role['name']) except exception.NotFound: pass try: super(RoleApi, self).update(id, role) except exception.NotFound: raise exception.UserNotFound(user_id=id)
def remove_user(self, user_dn, group_id, user_id): conn = self.get_connection() try: conn.modify_s( self._id_to_dn(group_id), [(ldap.MOD_DELETE, self.member_attribute, user_dn)]) except ldap.NO_SUCH_ATTRIBUTE: raise exception.UserNotFound(user_id=user_id) finally: conn.unbind_s()
def delete_user(self, user_id): try: old_user = self.db.get('user-%s' % user_id) except exception.NotFound: raise exception.UserNotFound(user_id=user_id) domain_id = self._user_id_to_domain_id.get(user_id) self.db.delete(self._calc_user_name_key(old_user['name'], domain_id)) self.db.delete('user-%s' % user_id) self._user_id_to_domain_id.notify_user_deleted(user_id) user_list = set(self.db.get('user_list', [])) user_list.remove(user_id) self.db.set('user_list', list(user_list))
def delete_user(self, user_id): session = self.get_session() try: ref = session.query(User).filter_by(id=user_id).one() except sql.NotFound: raise exception.UserNotFound(user_id=user_id) with session.begin(): q = session.query(UserTenantMembership) q = q.filter_by(user_id=user_id) q.delete(False) q = session.query(UserProjectMetadata) q = q.filter_by(user_id=user_id) q.delete(False) if not session.query(User).filter_by(id=user_id).delete(False): raise exception.UserNotFound(user_id=user_id) session.delete(ref) session.flush()
def create_trust(self, context, trust=None): """Create a new trust. The user creating the trust must be the trustor. """ # TODO(ayoung): instead of raising ValidationError on the first # problem, return a collection of all the problems. if not trust: raise exception.ValidationError(attribute='trust', target='request') try: user_id = self._get_user_id(context) _trustor_only(context, trust, user_id) #confirm that the trustee exists trustee_ref = self.identity_api.get_user(trust['trustee_user_id']) if not trustee_ref: raise exception.UserNotFound(user_id=trust['trustee_user_id']) all_roles = self.assignment_api.list_roles() clean_roles = self._clean_role_list(context, trust, all_roles) if trust.get('project_id'): user_role = self.assignment_api.get_roles_for_user_and_project( user_id, trust['project_id']) else: user_role = [] for trust_role in clean_roles: matching_roles = [x for x in user_role if x == trust_role['id']] if not matching_roles: raise exception.RoleNotFound(role_id=trust_role['id']) if trust.get('expires_at') is not None: if not trust['expires_at'].endswith('Z'): trust['expires_at'] += 'Z' try: trust['expires_at'] = (timeutils.parse_isotime (trust['expires_at'])) except ValueError: raise exception.ValidationTimeStampError() trust_id = uuid.uuid4().hex new_trust = self.trust_api.create_trust(trust_id, trust, clean_roles) self._fill_in_roles(context, new_trust, all_roles) return TrustV3.wrap_member(context, new_trust) except KeyError as e: raise exception.ValidationError(attribute=e.args[0], target='trust')
def update_user(self, context, user_id, user): # NOTE(termie): this is really more of a patch than a put self.assert_admin(context) if self.identity_api.get_user(context, user_id) is None: raise exception.UserNotFound(user_id=user_id) user_ref = self.identity_api.update_user(context, user_id, user) # If the password was changed or the user was disabled we clear tokens if user.get('password') or user.get('enabled', True) == False: try: for token_id in self.token_api.list_tokens(context, user_id): self.token_api.delete_token(context, token_id) except exception.NotImplemented: # The users status has been changed but tokens remain valid for # backends that can't list tokens for users LOG.warning('User %s status has changed, but existing tokens ' 'remain valid' % user_id) return {'user': user_ref}
def delete_user(self, role_dn, user_dn, tenant_dn, user_id, role_id): conn = self.get_connection() try: conn.modify_s(role_dn, [(ldap.MOD_DELETE, self.member_attribute, user_dn)]) except ldap.NO_SUCH_OBJECT: if tenant_dn is None: raise exception.RoleNotFound(role_id=role_id) attrs = [('objectClass', [self.object_class]), (self.member_attribute, [user_dn])] if self.use_dumb_member: attrs[1][1].append(self.dumb_member) try: conn.add_s(role_dn, attrs) except Exception as inst: raise inst except ldap.NO_SUCH_ATTRIBUTE: raise exception.UserNotFound(user_id=user_id)
def update_user(self, user_id, user): if 'name' in user: existing = self.db.get('user_name-%s' % user['name']) if existing and user_id != existing['id']: msg = 'Duplicate name, %s.' % user['name'] raise exception.Conflict(type='user', details=msg) # get the old name and delete it too try: old_user = self.db.get('user-%s' % user_id) except exception.NotFound: raise exception.UserNotFound(user_id=user_id) new_user = old_user.copy() user = utils.hash_user_password(user) new_user.update(user) if new_user['id'] != user_id: raise exception.ValidationError('Cannot change user ID') self.db.delete('user_name-%s' % old_user['name']) self.db.set('user-%s' % user_id, new_user) self.db.set('user_name-%s' % new_user['name'], new_user) return new_user
def update_user(self, user_id, user): if 'name' in user: user['name'] = clean.user_name(user['name']) session = self.get_session() if 'id' in user and user_id != user['id']: raise exception.ValidationError('Cannot change user ID') with session.begin(): user_ref = session.query(User).filter_by(id=user_id).first() if user_ref is None: raise exception.UserNotFound(user_id=user_id) old_user_dict = user_ref.to_dict() user = _ensure_hashed_password(user) for k in user: old_user_dict[k] = user[k] new_user = User.from_dict(old_user_dict) user_ref.name = new_user.name user_ref.extra = new_user.extra session.flush() return user_ref
def _get_federated_user(self, idp_id, protocol_id, unique_id): """Return the found user for the federated identity. :param idp_id: The identity provider ID :param protocol_id: The federation protocol ID :param unique_id: The user's unique ID (unique within the IdP) :returns User: Returns a reference to the User """ with sql.session_for_read() as session: query = session.query(model.User).outerjoin(model.LocalUser) query = query.join(model.FederatedUser) query = query.filter(model.FederatedUser.idp_id == idp_id) query = query.filter( model.FederatedUser.protocol_id == protocol_id) query = query.filter(model.FederatedUser.unique_id == unique_id) try: user_ref = query.one() except sql.NotFound: raise exception.UserNotFound(user_id=unique_id) return user_ref
def update_user(self, user_id, user): try: domain_id = self._user_id_to_domain_id.get(user_id) except exception.NotFound: raise exception.UserNotFound(user_id=user_id) if 'name' in user: user_key = self._calc_user_name_key(user['name'], domain_id) existing = self.db.get(user_key, False) if existing and user_id != existing['id']: msg = _('Duplicate name, %s.') % user['name'] raise exception.Conflict(type='user', details=msg) # get the old name and delete it too old_user = self.db.get('user-%s' % user_id) new_user = old_user.copy() user = utils.hash_user_password(user) new_user.update(user) self.db.delete(self._calc_user_name_key(old_user['name'], domain_id)) self.db.set('user-%s' % user_id, new_user) user_name_key = self._calc_user_name_key(new_user['name'], domain_id) self.db.set(user_name_key, new_user) return identity.filter_user(new_user)
def update_user(self, user_id, user): if 'name' in user: user['name'] = clean.user_name(user['name']) session = self.get_session() if 'id' in user and user_id != user['id']: raise exception.ValidationError('Cannot change user ID') with session.begin(): user_ref = session.query(User).filter_by(id=user_id).first() if user_ref is None: raise exception.UserNotFound(user_id=user_id) old_user_dict = user_ref.to_dict() user = utils.hash_user_password(user) for k in user: old_user_dict[k] = user[k] new_user = User.from_dict(old_user_dict) for attr in User.attributes: if attr != 'id': setattr(user_ref, attr, getattr(new_user, attr)) user_ref.extra = new_user.extra session.flush() return identity.filter_user(user_ref.to_dict(include_extra_dict=True))
def update(self, id, values): if values['id'] != id: raise exception.ValidationError('Cannot change user ID') try: old_obj = self.get(id) except exception.NotFound: raise exception.UserNotFound(user_id=id) if old_obj.get('name') != values['name']: raise exception.Conflict('Cannot change user name') try: new_tenant = values['tenant_id'] except KeyError: pass else: if old_obj.get('tenant_id') != new_tenant: if old_obj['tenant_id']: self.tenant_api.remove_user(old_obj['tenant_id'], id) if new_tenant: self.tenant_api.add_user(new_tenant, id) _ensure_hashed_password(values) super(UserApi, self).update(id, values, old_obj)
def get_user_roles(self, context, user_id, tenant_id=None): """Get the roles for a user and tenant pair. Since we're trying to ignore the idea of user-only roles we're not implementing them in hopes that the idea will die off. """ if tenant_id is None: raise exception.NotImplemented(message='User roles not supported: ' 'tenant ID required') user = self.identity_api.get_user(context, user_id) if user is None: raise exception.UserNotFound(user_id=user_id) tenant = self.identity_api.get_tenant(context, tenant_id) if tenant is None: raise exception.TenantNotFound(tenant_id=tenant_id) roles = self.identity_api.get_roles_for_user_and_tenant( context, user_id, tenant_id) return {'roles': [self.identity_api.get_role(context, x) for x in roles]}
def get(self, id, filter=None): """Replaces exception.NotFound with exception.UserNotFound.""" try: return super(UserApi, self).get(id, filter) except exception.NotFound: raise exception.UserNotFound(user_id=id)
def _get_user_by_name(self, user_name): session = self.get_session() user_ref = session.query(User).filter_by(name=user_name).first() if not user_ref: raise exception.UserNotFound(user_id=user_name) return user_ref.to_dict()
def _get_user(self, session, user_id): user_ref = session.query(model.User).get(user_id) if not user_ref: raise exception.UserNotFound(user_id=user_id) return user_ref
def _get_user_by_name(self, user_name, domain_id): try: return self.db.get('user_name-%s' % user_name) except exception.NotFound: raise exception.UserNotFound(user_id=user_name)
def _get_user(self, user_id): try: return self.db.get('user-%s' % user_id) except exception.NotFound: raise exception.UserNotFound(user_id=user_id)
def delete_user(self, context, user_id): self.assert_admin(context) if self.identity_api.get_user(context, user_id) is None: raise exception.UserNotFound(user_id=user_id) self.identity_api.delete_user(context, user_id)
def _get_user_by_name(self, user_name, domain_id): try: return self.db.get(self._calc_user_name_key(user_name, domain_id)) except exception.NotFound: raise exception.UserNotFound(user_id=user_name)
def get_user(self, user_id): raise exception.UserNotFound(user_id=user_id)
def get_user(self, user_id): user_ref, session = get_user_session(user_id) if not user_ref: raise exception.UserNotFound(user_id=user_id) return user_ref
def delete_user(self, user_id): try: return self.user.delete(user_id) except ldap.NO_SUCH_OBJECT: raise exception.UserNotFound(user_id=user_id)
def get_user_by_name(self, user_name): try: return _filter_user(self.user.get_by_name(user_name)) except exception.NotFound: raise exception.UserNotFound(user_id=user_name)