-
Notifications
You must be signed in to change notification settings - Fork 174
/
BootStrap.groovy
executable file
·97 lines (83 loc) · 3.64 KB
/
BootStrap.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import grails.util.Environment
import org.apache.commons.codec.digest.DigestUtils
import org.grails.auth.Role
import org.grails.auth.User
import javax.imageio.ImageIO
class BootStrap {
def fixtureLoader
def searchableService
def init = { servletContext ->
// disable JAI caching which causes problems, http://stackoverflow.com/questions/18517463/jai-create-seems-to-leave-file-descriptors-open
// JAI is used in burning image plugin for image scaling
ImageIO.setUseCache(false)
User.withNewTransaction { doInit() }
}
void doInit() {
def (adminRole, editorRole, observerRole) = setUpRoles()
def admin = User.findByLogin("admin")
if (!admin) {
def password = Environment.current != Environment.PRODUCTION ? "changeit" : System.getenv("ADMIN_PASSWORD")
if (!password) {
throw new Exception("""
During the first run you must specify a password to use for the admin account. For example:
grails -Dinitial.admin.password=changeit -Dload.fixtures=true prod run-app""")
}
else {
admin = new User(login:"admin", email:"info@g2one.com",password:DigestUtils.shaHex(password))
admin.save(flush: true, failOnError: true)
assert admin.email
assert admin.addToRoles(adminRole)
.addToRoles(editorRole)
.addToRoles(observerRole)
.save(flush:true, failOnError: true)
}
}
else if (!admin.roles) {
admin.addToRoles(adminRole)
.addToRoles(editorRole)
.addToRoles(observerRole)
.save(flush:true, failOnError: true)
}
// Load dev data to make it easier to work on the application.
if ((System.getProperty("load.fixtures")
|| Environment.current == Environment.DEVELOPMENT
|| Environment.current == Environment.TEST) && User.count() < 2) {
println "Loading fixture data"
fixtureLoader.with {
load("users").load("plugins").load("tags", "ratings")
[
'wiki',
].each {
load(it)
}
}
}
// We manually start the mirroring process to ensure that it comes after
// Autobase performs its migrations.
if (Environment.current != Environment.DEVELOPMENT || System.getProperty("reindex")) {
println "Performing bulk index"
searchableService.reindex()
}
println "Starting mirror service"
searchableService.startMirroring()
}
def destroy = {
}
private setUpRoles() {
// Admin role first. Adminstrator can access all parts of the application.
def admin = Role.findByName(Role.ADMINISTRATOR) ?: new Role(name: Role.ADMINISTRATOR).save(failOnError: true)
safelyAddPermission admin, "*"
// Editor can edit pages, etc.
def editor = Role.findByName(Role.EDITOR) ?: new Role(name: Role.EDITOR).save(failOnError: true)
safelyAddPermission editor, "pluginTab:editWikiPage"
safelyAddPermission editor, "likeDislike:like,dislike"
// Observer: can't do anything that an anonymous user can't do.
def observer = Role.findByName(Role.OBSERVER) ?: new Role(name: Role.OBSERVER).save(failOnError: true)
return [admin, editor, observer]
}
private safelyAddPermission(entity, String permission) {
if (!entity.permissions?.contains(permission)) {
entity.addToPermissions permission
}
}
}