Search in sources :

Example 21 with RunAsSystem

use of org.molgenis.security.core.runas.RunAsSystem in project molgenis by molgenis.

the class IndexActionRegisterServiceImpl method storeIndexActions.

@Override
@RunAsSystem
public void storeIndexActions(String transactionId) {
    Set<Impact> changes = getChangesForCurrentTransaction();
    if (changes.isEmpty()) {
        return;
    }
    if (changes.stream().allMatch(impact -> excludedEntities.contains(impact.getEntityTypeId()))) {
        return;
    }
    IndexActionGroup indexActionGroup = indexActionGroupFactory.create(transactionId);
    IndexDependencyModel dependencyModel = createIndexDependencyModel(changes);
    Stream<Impact> impactStream = indexingStrategy.determineImpact(changes, dependencyModel).stream().filter(key -> !excludedEntities.contains(key.getEntityTypeId()));
    List<IndexAction> indexActions = mapWithIndex(impactStream, (key, actionOrder) -> createIndexAction(indexActionGroup, key, (int) actionOrder)).collect(toList());
    if (indexActions.isEmpty()) {
        return;
    }
    LOG.debug("Store index actions for transaction {}", transactionId);
    dataService.add(INDEX_ACTION_GROUP, indexActionGroupFactory.create(transactionId).setCount(indexActions.size()));
    dataService.add(INDEX_ACTION, indexActions.stream());
}
Also used : Impact.createSingleEntityImpact(org.molgenis.data.index.Impact.createSingleEntityImpact) PENDING(org.molgenis.data.index.meta.IndexActionMetaData.IndexStatus.PENDING) LoggerFactory(org.slf4j.LoggerFactory) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) IndexActionFactory(org.molgenis.data.index.meta.IndexActionFactory) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) TRANSACTION_ID_RESOURCE_NAME(org.molgenis.data.transaction.TransactionManager.TRANSACTION_ID_RESOURCE_NAME) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) HashMultimap(com.google.common.collect.HashMultimap) EntityKey(org.molgenis.data.EntityKey) Impact.createSingleEntityImpact(org.molgenis.data.index.Impact.createSingleEntityImpact) Objects.requireNonNull(java.util.Objects.requireNonNull) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) REF_ENTITY_TYPE(org.molgenis.data.meta.model.AttributeMetadata.REF_ENTITY_TYPE) Multimaps.synchronizedSetMultimap(com.google.common.collect.Multimaps.synchronizedSetMultimap) Collectors.toSet(java.util.stream.Collectors.toSet) INDEX_ACTION(org.molgenis.data.index.meta.IndexActionMetaData.INDEX_ACTION) Streams.mapWithIndex(com.google.common.collect.Streams.mapWithIndex) IndexActionGroup(org.molgenis.data.index.meta.IndexActionGroup) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) INDEX_ACTION_GROUP(org.molgenis.data.index.meta.IndexActionGroupMetaData.INDEX_ACTION_GROUP) IndexAction(org.molgenis.data.index.meta.IndexAction) Set(java.util.Set) TransactionInformation(org.molgenis.data.transaction.TransactionInformation) EntityType(org.molgenis.data.meta.model.EntityType) SetMultimap(com.google.common.collect.SetMultimap) Sets(com.google.common.collect.Sets) Component(org.springframework.stereotype.Component) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) IndexActionGroupFactory(org.molgenis.data.index.meta.IndexActionGroupFactory) DataService(org.molgenis.data.DataService) Optional(java.util.Optional) ENTITY_TYPE_FETCH(org.molgenis.data.index.IndexDependencyModel.ENTITY_TYPE_FETCH) Transactional(org.springframework.transaction.annotation.Transactional) IndexActionGroup(org.molgenis.data.index.meta.IndexActionGroup) IndexAction(org.molgenis.data.index.meta.IndexAction) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 22 with RunAsSystem

use of org.molgenis.security.core.runas.RunAsSystem in project molgenis by molgenis.

the class AlgorithmServiceImpl method autoGenerateAlgorithm.

@Override
@RunAsSystem
public void autoGenerateAlgorithm(EntityType sourceEntityType, EntityType targetEntityType, EntityMapping mapping, Attribute targetAttribute) {
    LOG.debug("createAttributeMappingIfOnlyOneMatch: target= " + targetAttribute.getName());
    Multimap<Relation, OntologyTerm> tagsForAttribute = ontologyTagService.getTagsForAttribute(targetEntityType, targetAttribute);
    Map<Attribute, ExplainedAttribute> relevantAttributes = semanticSearchService.decisionTreeToFindRelevantAttributes(sourceEntityType, targetAttribute, tagsForAttribute.values(), null);
    GeneratedAlgorithm generatedAlgorithm = algorithmGeneratorService.generate(targetAttribute, relevantAttributes, targetEntityType, sourceEntityType);
    if (StringUtils.isNotBlank(generatedAlgorithm.getAlgorithm())) {
        AttributeMapping attributeMapping = mapping.addAttributeMapping(targetAttribute.getName());
        attributeMapping.setAlgorithm(generatedAlgorithm.getAlgorithm());
        attributeMapping.getSourceAttributes().addAll(generatedAlgorithm.getSourceAttributes());
        attributeMapping.setAlgorithmState(generatedAlgorithm.getAlgorithmState());
        LOG.debug("Creating attribute mapping: " + targetAttribute.getName() + " = " + generatedAlgorithm.getAlgorithm());
    }
}
Also used : Relation(org.molgenis.data.semantic.Relation) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) GeneratedAlgorithm(org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 23 with RunAsSystem

use of org.molgenis.security.core.runas.RunAsSystem in project molgenis by molgenis.

the class UserDetailsService method loadUserByUsername.

@Override
@RunAsSystem
public UserDetails loadUserByUsername(String username) {
    User user = dataService.query(UserMetaData.USER, User.class).eq(UserMetaData.USERNAME, username).findOne();
    if (user == null) {
        throw new UsernameNotFoundException("unknown user '" + username + "'");
    }
    Collection<? extends GrantedAuthority> authorities = getAuthorities(user);
    return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.isActive(), true, true, true, authorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.molgenis.data.security.auth.User) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 24 with RunAsSystem

use of org.molgenis.security.core.runas.RunAsSystem in project molgenis by molgenis.

the class TokenAuthenticationProvider method authenticate.

@Override
@RunAsSystem
public Authentication authenticate(Authentication authentication) {
    if (!supports(authentication.getClass()))
        throw new IllegalArgumentException("Only RestAuthenticationToken is supported");
    RestAuthenticationToken authToken = (RestAuthenticationToken) authentication;
    if (authToken.getToken() != null) {
        // Throws UnknownTokenException
        UserDetails userDetails = tokenService.findUserByToken(authToken.getToken());
        // if token is invalid
        authToken = new RestAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities(), authToken.getToken());
    }
    return authToken;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 25 with RunAsSystem

use of org.molgenis.security.core.runas.RunAsSystem in project molgenis by molgenis.

the class AccountServiceImpl method createUser.

@Override
@RunAsSystem
@Transactional
public void createUser(User user, String baseActivationUri) throws UsernameAlreadyExistsException, EmailAlreadyExistsException {
    // Check if username already exists
    if (userService.getUser(user.getUsername()) != null) {
        throw new UsernameAlreadyExistsException("Username '" + user.getUsername() + "' already exists.");
    }
    // Check if email already exists
    if (userService.getUserByEmail(user.getEmail()) != null) {
        throw new EmailAlreadyExistsException("Email '" + user.getEmail() + "' is already registered.");
    }
    // collect activation info
    String activationCode = idGenerator.generateId(SECURE_RANDOM);
    List<String> activationEmailAddresses;
    if (authenticationSettings.getSignUpModeration()) {
        activationEmailAddresses = userService.getSuEmailAddresses();
        if (activationEmailAddresses == null || activationEmailAddresses.isEmpty())
            throw new MolgenisDataException("Administrator account is missing required email address");
    } else {
        String activationEmailAddress = user.getEmail();
        if (activationEmailAddress == null || activationEmailAddress.isEmpty())
            throw new MolgenisDataException("User '" + user.getUsername() + "' is missing required email address");
        activationEmailAddresses = asList(activationEmailAddress);
    }
    // create user
    user.setActivationCode(activationCode);
    user.setActive(false);
    dataService.add(USER, user);
    LOG.debug("created user " + user.getUsername());
    // add user to group
    Group group = dataService.query(GROUP, Group.class).eq(NAME, ALL_USER_GROUP).findOne();
    GroupMember groupMember = null;
    if (group != null) {
        groupMember = groupMemberFactory.create();
        groupMember.setGroup(group);
        groupMember.setUser(user);
        dataService.add(GROUP_MEMBER, groupMember);
    }
    // send activation email
    URI activationUri = URI.create(baseActivationUri + '/' + activationCode);
    try {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(activationEmailAddresses.toArray(new String[] {}));
        mailMessage.setSubject("User registration for " + appSettings.getTitle());
        mailMessage.setText(createActivationEmailText(user, activationUri));
        mailSender.send(mailMessage);
    } catch (MailException mce) {
        LOG.error("Could not send signup mail", mce);
        if (groupMember != null) {
            dataService.delete(GROUP_MEMBER, groupMember);
        }
        dataService.delete(USER, user);
        throw new MolgenisUserException("An error occurred. Please contact the administrator. You are not signed up!");
    }
    LOG.debug("send activation email for user " + user.getUsername() + " to " + StringUtils.join(activationEmailAddresses, ','));
}
Also used : Group(org.molgenis.data.security.auth.Group) GroupMember(org.molgenis.data.security.auth.GroupMember) MolgenisDataException(org.molgenis.data.MolgenisDataException) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) MailException(org.springframework.mail.MailException) URI(java.net.URI) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)29 Entity (org.molgenis.data.Entity)12 Transactional (org.springframework.transaction.annotation.Transactional)7 User (org.molgenis.data.security.auth.User)5 QueryImpl (org.molgenis.data.support.QueryImpl)4 UnknownEntityException (org.molgenis.data.UnknownEntityException)3 MolgenisUserException (org.molgenis.security.user.MolgenisUserException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Stream (java.util.stream.Stream)2 DataService (org.molgenis.data.DataService)2 Fetch (org.molgenis.data.Fetch)2 IndexActionGroup (org.molgenis.data.index.meta.IndexActionGroup)2 INDEX_ACTION_GROUP (org.molgenis.data.index.meta.IndexActionGroupMetaData.INDEX_ACTION_GROUP)2 Token (org.molgenis.data.security.auth.Token)2 ProgressImpl (org.molgenis.jobs.ProgressImpl)2 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)2 RunAsUserToken (org.springframework.security.access.intercept.RunAsUserToken)2