-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/SHIBUI-1031' of bitbucket.org:unicon/shib-idp-u…
…i into feature/SHIBUI-1031
- Loading branch information
Showing
21 changed files
with
239 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
backend/src/main/groovy/edu/internet2/tier/shibboleth/admin/ui/service/UserBootstrap.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service | ||
|
||
import com.opencsv.CSVReader | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.ShibUIConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.security.model.Role | ||
import edu.internet2.tier.shibboleth.admin.ui.security.model.User | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.RoleRepository | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository | ||
import groovy.util.logging.Slf4j | ||
import org.springframework.boot.context.event.ApplicationStartedEvent | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.stereotype.Component | ||
|
||
import javax.transaction.Transactional | ||
|
||
@Component | ||
@Slf4j | ||
class UserBootstrap { | ||
private final ShibUIConfiguration shibUIConfiguration | ||
private final UserRepository userRepository | ||
private final RoleRepository roleRepository | ||
|
||
UserBootstrap(ShibUIConfiguration shibUIConfiguration, UserRepository userRepository, RoleRepository roleRepository) { | ||
this.shibUIConfiguration = shibUIConfiguration | ||
this.userRepository = userRepository | ||
this.roleRepository = roleRepository | ||
} | ||
|
||
@Transactional | ||
@EventListener | ||
void bootstrapUsersAndRoles(ApplicationStartedEvent e) { | ||
if (shibUIConfiguration.userBootstrapResource) { | ||
log.info("configuring users from ${shibUIConfiguration.userBootstrapResource.URI}") | ||
new CSVReader(new InputStreamReader(shibUIConfiguration.userBootstrapResource.inputStream)).each { it -> | ||
def (username, password, firstName, lastName, roleName) = it | ||
def role = roleRepository.findByName(roleName).orElse(roleRepository.save(new Role(name: roleName))) | ||
def user = userRepository.findByUsername(username).orElse(new User(username: username)).with { | ||
it.password = password | ||
it.firstName = firstName | ||
it.lastName = lastName | ||
it.roles.add(role) | ||
it | ||
} | ||
userRepository.save(user) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../src/test/groovy/edu/internet2/tier/shibboleth/admin/ui/service/UserBootstrapTests.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.service | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.configuration.CoreShibUiConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.InternationalizationConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.SearchConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.ShibUIConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.configuration.TestConfiguration | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.RoleRepository | ||
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.domain.EntityScan | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest | ||
import org.springframework.core.io.ClassPathResource | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories | ||
import org.springframework.test.annotation.DirtiesContext | ||
import org.springframework.test.context.ContextConfiguration | ||
import spock.lang.Specification | ||
|
||
@DataJpaTest | ||
@ContextConfiguration(classes=[CoreShibUiConfiguration, SearchConfiguration, TestConfiguration, InternationalizationConfiguration, ShibUIConfiguration]) | ||
@EnableJpaRepositories(basePackages = ["edu.internet2.tier.shibboleth.admin.ui"]) | ||
@EntityScan(["edu.internet2.tier.shibboleth.admin.ui", "edu.internet2.tier.shibboleth.admin.ui.security.model"]) | ||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD) | ||
class UserBootstrapTests extends Specification { | ||
@Autowired | ||
ShibUIConfiguration shibUIConfiguration | ||
|
||
@Autowired | ||
UserRepository userRepository | ||
|
||
@Autowired | ||
RoleRepository roleRepository | ||
|
||
def "simple test"() { | ||
setup: | ||
shibUIConfiguration.userBootstrapResource = new ClassPathResource('/conf/1044.csv') | ||
def userBootstrap = new UserBootstrap(shibUIConfiguration, userRepository, roleRepository) | ||
|
||
when: | ||
userBootstrap.bootstrapUsersAndRoles(null) | ||
|
||
then: | ||
noExceptionThrown() | ||
assert userRepository.findAll().size() == 2 | ||
assert roleRepository.findAll().size() == 2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"user1","password1","firstName1","lastName1","ROLE_ADMIN" | ||
"user2","password2","firstName2","lastName2","ROLE_USER" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.