/
JSecurityAuthFilters.groovy
168 lines (153 loc) · 5.34 KB
/
JSecurityAuthFilters.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package org.grails.auth
import org.apache.shiro.SecurityUtils
/**
* @author Graeme Rocher
* @since 1.0
*
* Created: Feb 26, 2008
*/
class JSecurityAuthFilters {
/**
* Called when an unauthenticated user tries to access a secured
* page.
*/
def onNotAuthenticated(subject, d) {
def targetUri = d.request.forwardURI
if (d.request.queryString) {
targetUri = "${targetUri}?${d.request.queryString}"
}
if (d.request.xhr) {
d.render(template:"/user/loginForm", model:[targetUri:targetUri,
formData:d.params,
async:true,
update:d.params._ul,
message:"auth.not.logged.in"])
} else if(d.response.format == 'text') {
d.render status:401, text:"Permission Denied"
}else {
// Redirect to login page.
d.redirect(
controller: 'user',
action: 'login',
params:[targetUri: targetUri])
}
}
/**
* Called when an authenticated user tries to access a page that they don't
* have the rights for.
*/
def onUnauthorized(subject, d) {
if (d.request.xhr) {
d.render "You do not have permission to access this page."
} else {
// Redirect to the 'unauthorized' page.
d.redirect controller: 'user', action: 'unauthorized'
}
}
static filters = {
def requiresPermissions = [
pluginTab: ["editWikiPage"],
tutorial: ["create", "edit", "save", "update"] as Set,
webSite: ["create", "edit", "save", "update"] as Set,
likeDislike: ["like", "dislike"] as Set ]
withPermissions(controller: "*", action: "*") {
before = {
if (actionName in requiresPermissions[controllerName]) {
accessControl()
}
else {
return true
}
}
}
// Ensure that all controllers and actions require an authenticated user,
// Creating, modifying, or deleting a book requires the "Administrator"
// role.
wikiEditing(controller: "(content|news|plugin)", action: "(editNews|createNews|markupWikiPage|editWikiPage|createWikiPage|saveWikiPage|editPlugin|createPlugin|uploadImage|addTag|removeTag)") {
before = {
accessControl {
role("Editor") || role("Administrator")
}
}
}
jobPosting(controller:"(job|paypal)", action:"(delete|edit|update|editJobs|save|create|buy|success|cancel)") {
before = {
accessControl {
role("Editor") || role("Administrator")
}
}
}
wikiManagement(controller:"content", action:"rollbackWikiVersion") {
before = {
accessControl {
role("Administrator")
}
}
}
userProfile(controller:"user", action:"profile") {
before = {
accessControl {
role("Editor") || role("Administrator")
}
}
}
comments(controller:"commentable", action:"add") {
before = {
accessControl { true }
}
}
pluginPublishing(controller:"repository", action:"publish") {
before = {
accessControl { permission "plugin:publish:${params.plugin}" }
}
}
screencasts(controller:"screencast", action:"(edit|create|save|update)") {
before = {
accessControl { true }
}
}
blogPosting(controller:"blog", action:"(createEntry|editEntry)") {
before = {
accessControl { true }
}
}
blogDeletion(controller:"blog", action:"delete") {
before = {
accessControl {
role("Administrator")
}
}
}
pluginDeletion(controller:"plugin", action:"deletePlugin") {
before = {
accessControl {
role("Administrator")
}
}
}
pluginActivities(controller:"(tag|plugin|rateable)", action:"(update|postComment|autoCompleteNames|rate)") {
before = {
accessControl {
role("Editor") || role("Administrator")
}
}
}
adminArea(uri:"/admin/**") {
before = {
if (controllerName == "error") return true
accessControl {
role("Administrator")
}
}
}
userInRequest(controller:"*", action:"*") {
before = {
if (controllerName == "error") return true
def subject = SecurityUtils.getSubject()
if(subject && subject?.principal) {
request.user = User.findByLogin(subject.principal, [cache:true])
}
}
}
}
}