124 lines
7.5 KiB
HTML
124 lines
7.5 KiB
HTML
<!doctype html>
|
||
<html>
|
||
<head>
|
||
<title>Zk | 2010-07-25 20:09:29</title>
|
||
<link rel="stylesheet" type="text/css" href="/style.css" />
|
||
<meta name="viewport" content="width=device-width" />
|
||
<meta charset="utf-8" />
|
||
</head>
|
||
<body>
|
||
<main>
|
||
<header>
|
||
<h1>Zk | 2010-07-25 20:09:29</h1>
|
||
</header>
|
||
<article class="content">
|
||
<p><span class="tag">blog</span> <span class="tag">fossil</span> <span class="tag">diary</span></p>
|
||
<p>Working on a rather large project in grails, I've come to realize two things: there is some absolutely amazing software frameworks out there, and some people who write documentation appear to be 3-year-old kids who speak English as a second language. Â The embarrassing part about that is that, since the project is open-source, I could contribute to the documentation very easily, fixing problems that I see and adding where I see fit. Â However, the problem is so large and daunting (and the project I'm working on way more interesting), so instead, I wind up just living with it and searching El Goog over and over again for the same things.</p>
|
||
<p>I'm going to try to change this as best I can, and I'm going to start by collecting a few nifty tips and tricks I've pulled out of thin air here, and hopefully pull them all together into one place soon enough.</p>
|
||
<!--more-->
|
||
|
||
<h2>Permissions</h2>
|
||
|
||
<p>The current project is using the grails Acegi plugin to manage users and security. Â The plugin was recently deprecated, but it appears to have been done so before the replacement was completed, so I'm using it for the time being. Â The idea of roles is pretty standard, but I wanted some finer-grained control over permissions to specific views, especially pertaining to objects that have lists of users associated with them. Â After fiddling around with specifics in the controllers, I abstracted it into a service that I can use everywhere:
|
||
<pre lang="enc__groovy">grails-app/services/package/PermissionsService.groovy
|
||
class PermissionsService {
|
||
static transactional = true</p>
|
||
<div class="codehilite"><pre><span></span><code><span class="nv">def</span> <span class="nv">authenticateService</span>
|
||
|
||
<span class="nv">def</span> <span class="nv">groups</span> <span class="o">=</span> [
|
||
<span class="nv">userCanRead</span>: { <span class="nv">group</span> <span class="o">-></span>
|
||
<span class="k">if</span> <span class="ss">(</span><span class="nv">group</span>.<span class="nv">exclusive</span><span class="ss">)</span> {
|
||
<span class="k">if</span> <span class="ss">(</span><span class="nv">authenticateService</span>.<span class="nv">principal</span><span class="ss">()</span>.<span class="nv">domainClass</span> <span class="nv">in</span> <span class="nv">group</span>.<span class="nv">members</span>
|
||
<span class="o">||</span> <span class="nv">authenticateService</span>.<span class="nv">principal</span><span class="ss">()</span>.<span class="nv">domainClass</span>.<span class="nv">id</span> <span class="o">==</span> <span class="nv">group</span>.<span class="nv">admin</span>.<span class="nv">id</span>
|
||
<span class="o">||</span> <span class="nv">authenticateService</span>.<span class="nv">ifAnyGranted</span><span class="ss">(</span><span class="s2">"</span><span class="s">ROLE_ADMIN</span><span class="s2">"</span><span class="ss">))</span> {
|
||
<span class="k">return</span> <span class="nv">true</span>
|
||
}
|
||
} <span class="k">else</span> {
|
||
<span class="k">return</span> <span class="nv">true</span>
|
||
}
|
||
},
|
||
<span class="o">//</span> ...
|
||
]
|
||
</code></pre></div>
|
||
|
||
|
||
<p>}</pre>
|
||
As you can see, the service contains a list of permissions - closures that do a few tests and return true or false - organized into lists in order to separate them into logical groups (in this case, the groups list pertains to the groups domain class, controller, and views). Each closure expects one argument - the object to test the permissions of. This comes in handy for the corresponding TagLib:
|
||
<pre lang="enc__groovy">grails-app/taglib/package/PermissionsTagLib.groovy
|
||
class PermissionsTagLib {
|
||
static namespace = "my"
|
||
def permissionsService
|
||
def withPermission = { attrs, body ->
|
||
if (permissionsService."${attrs['class']}"."${attrs['permission']}"(attrs['arg'])) {
|
||
out << body()
|
||
}
|
||
}
|
||
}</pre>
|
||
The tag-lib allows us to write logical tags that will only output data if the user passes the test, i.e:
|
||
<pre lang="enc__html">In a view...
|
||
<my:withPermission class="groups" permission="userCanPost" arg="${group}">
|
||
Post new topic
|
||
</my:withPermission></pre>
|
||
After all, it is nice to ask permission...
|
||
<h2>Comma-separated tags</h2>
|
||
Just a little snippet, but I'm using comma-separated tags for tagging some domains, and I wasn't really keen on some of the solutions I saw out there, so I scribbled out a 'one-liner' for tagging stuff:
|
||
<pre lang="enc__groovy">grails-app/services/package/TagService.groovy
|
||
tags.split(/(?!(?<=\)),/)
|
||
.collect { it.trim().replaceAll(/\,/, ",") }
|
||
.each {
|
||
if (it.size() > 0) {
|
||
obj.tags.each { otag ->
|
||
if (it.tag == it) {
|
||
// skip if we already have it tagged
|
||
return
|
||
}
|
||
}
|
||
def t
|
||
if (Tag.countByTag(it) > 0) {
|
||
t = Tag.findByTag(it)
|
||
} else {
|
||
t = new Tag(tag: it).save()
|
||
}
|
||
obj.addToTags(t).save(flush: true)
|
||
}
|
||
}
|
||
// ...</pre>
|
||
This way, we can even have commas in tags, via: "foo, bar\, baz" (tags: ["foo", "bar, baz"])
|
||
<h2>Tips</h2>
|
||
<ul>
|
||
<li>Codecs are static (I forgot. Â You ever forget? Â Happened to me). Â I wasted a crapload of time on a problem in there before I switched to a tag-lib, <em>et voila</em>, everything's fixed</li>
|
||
<li>Constraints are also static, so if you want to constrain yourself to a list of strings, but want to be able to change the strings, store an integer and use that as an index to an array of strings stored in Config. Â For bonus points, store a portion of a property key so you can internationalize, and use the portion of the key as the default value.</li>
|
||
<li>If you want to always ensure that a condition is met when querying, stick the query in a service and pass the service a closure of your criteria:</li>
|
||
</ul>
|
||
<pre lang="groovy">In your service...
|
||
// ...
|
||
def ListWithRating (Closure c) {
|
||
Obj.withCriteria {
|
||
and {
|
||
c.delegate = delegate
|
||
c()
|
||
le('rating', maxRating)
|
||
}
|
||
}
|
||
}</pre>
|
||
<pre lang="groovy">In your controller...
|
||
// ...
|
||
def criteria = {
|
||
eq('type', params.type)
|
||
}
|
||
listService.listWithRating(criteria)</pre>
|
||
<ul>
|
||
<li>If you want to associate something with anything (i.e.: a comment with any other domain), store the domain's class name (with <code>obj.class.toString().split(/\./)[-1]</code>) with the comment, along with the object's id, then get it back the same way. Â You can check that the object exists with dynamic class-loading:</li>
|
||
</ul>
|
||
<pre lang="groovy">
|
||
def object = Class.forName("package.${objectType}", true, Thread.currentThread().getContextClassLoader())
|
||
.get(objectId)</pre>
|
||
That's about all for now, but I'm sure as the project progresses, I'll come up with more and I'll either post about them or collect them somewhere.</p>
|
||
</article>
|
||
<footer>
|
||
<p>Page generated on On Grails</p>
|
||
</footer>
|
||
</main>
|
||
</body>
|
||
</html>
|