All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.n2oapp.security.admin.impl.userinfo.UserInfoService Maven / Gradle / Ivy

There is a newer version: 8.0.1
Show newest version
package net.n2oapp.security.admin.impl.userinfo;

import net.n2oapp.security.admin.api.oauth.UserInfoEnricher;
import net.n2oapp.security.admin.impl.entity.AccountEntity;
import net.n2oapp.security.admin.impl.entity.UserEntity;
import net.n2oapp.security.admin.impl.repository.AccountRepository;
import net.n2oapp.security.admin.impl.repository.UserRepository;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static java.util.Objects.nonNull;

/**
 * Сервис для построения UserInfo
 */
@Transactional
public class UserInfoService {
    private final UserRepository userRepository;
    private final AccountRepository accountRepository;
    private final Collection> userInfoEnrichers;
    private final SimpleUserDataEnricher simpleUserDataEnricher = new SimpleUserDataEnricher();

    private static final String ACCOUNT_ID = "accountId";

    public UserInfoService(UserRepository userRepository, AccountRepository accountRepository, Collection> userInfoEnrichers) {
        this.userRepository = userRepository;
        this.accountRepository = accountRepository;
        this.userInfoEnrichers = userInfoEnrichers;
    }

    public Map buildUserInfo(Integer accountId) {
        Map userInfo = new HashMap<>();
        if (nonNull(accountId)) {
            AccountEntity account = accountRepository.getReferenceById(accountId);
            userInfo.put(ACCOUNT_ID, account.getId().toString());
            for (UserInfoEnricher enricher : userInfoEnrichers)
                enricher.enrich(userInfo, account);
            UserEntity user = userRepository.findOneByUsernameIgnoreCase(account.getUser().getUsername());
            simpleUserDataEnricher.enrich(userInfo, user);
        }
        return userInfo;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy