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

net.yadaframework.security.components.YadaUserSetup Maven / Gradle / Ivy

There is a newer version: 0.7.7.R4
Show newest version
package net.yadaframework.security.components;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import net.yadaframework.components.YadaSetup;
import net.yadaframework.core.YadaAppConfig;
import net.yadaframework.core.YadaConfiguration;
import net.yadaframework.security.persistence.entity.YadaUserProfile;
import net.yadaframework.security.persistence.repository.YadaUserCredentialsDao;

/**
 * Convenience method to create configured application users.
 * You should just create a @Component that extends this class and add a setup section to the configuration
 * with any user attribute you have implemented in your YadaUserProfile subclass.
 * For example:
 * 
 * 	<setup>
		<users>
			<user>
				<name>admin</name>
				<email>[email protected]</email>
				<password>25345352543154</password>
				<language>en</language>
				<country>US</country>
				<timezone>PST</timezone>
				<role>USER</role>
				<role>ADMIN</role>
			</user>
		</users>
	</setup>
	
* * @param the application-specific extension of YadaUserProfile */ //Not a @Component abstract public class YadaUserSetup extends YadaSetup { private transient Logger log = LoggerFactory.getLogger(YadaUserSetup.class); @Autowired private YadaUserCredentialsDao yadaUserCredentialsDao; @Autowired private YadaConfiguration yadaConfiguration; @Override protected void setupApplication() { // Default empty method log.debug("setupApplication() does nothing"); } @Override protected void setupUsers(List> userList) { if (YadaAppConfig.getStaticConfig().isDatabaseEnabled()) { // Retrieve the generics YadaUserProfile subclass: https://stackoverflow.com/a/75345/587641 Class userProfileClass = (Class)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; // for (Map userDefinition : userList) { yadaUserCredentialsDao.create(userDefinition, userProfileClass); if (yadaConfiguration.isDevelopmentEnvironment()) { log.info("User \"{}\", password \"{}\"", userDefinition.get("email"), userDefinition.getOrDefault("password", "***")); } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy