1 | package dbnp.configuration |
---|
2 | import dbnp.authentication.* |
---|
3 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
4 | |
---|
5 | /** |
---|
6 | * @Author Jeroen Wesbeek <work@osx.eu> |
---|
7 | * @Since 20101111 |
---|
8 | * |
---|
9 | * Revision information: |
---|
10 | * $Rev: 1812 $ |
---|
11 | * $Author: work@osx.eu $ |
---|
12 | * $Date: 2011-05-04 15:25:03 +0000 (wo, 04 mei 2011) $ |
---|
13 | */ |
---|
14 | class BootStrapAuthentication { |
---|
15 | /** |
---|
16 | * set up the initial roles and users if required |
---|
17 | * @visibility public |
---|
18 | * @void |
---|
19 | */ |
---|
20 | public static void initDefaultAuthentication(springSecurityService) { |
---|
21 | // Grom a development message |
---|
22 | if (String.metaClass.getMetaMethod("grom")) "setting up default authentication".grom() |
---|
23 | |
---|
24 | // user work variable |
---|
25 | def user=null |
---|
26 | |
---|
27 | // get configuration |
---|
28 | def config = ConfigurationHolder.config |
---|
29 | |
---|
30 | // create the admin role |
---|
31 | def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(flush:true, failOnError:true) |
---|
32 | |
---|
33 | // iterate through default users, see |
---|
34 | // - grails-app/conf/config-environment.properties |
---|
35 | // - ~/.grails-config/environment-gscf.properties |
---|
36 | config.authentication.users.each { key, values -> |
---|
37 | // make sure we do not add duplicate users |
---|
38 | if (!SecUser.findAllByUsername(values.username)) { |
---|
39 | // create user instance |
---|
40 | user = new SecUser( |
---|
41 | username:values.username, |
---|
42 | password:springSecurityService.encodePassword( values.password , values.username ), |
---|
43 | email:values.email, |
---|
44 | userConfirmed: true, |
---|
45 | adminConfirmed: true |
---|
46 | ).save(flush:true, failOnError: true) |
---|
47 | if (String.metaClass.getMetaMethod("grom")) "adding user ${values.username}".grom() |
---|
48 | |
---|
49 | // is this user an administrator? |
---|
50 | if (values.administrator == 'true') { |
---|
51 | SecUserSecRole.create(user, adminRole, true) |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|