Skip to content

Commit

Permalink
here is the 0.3 version
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdavis99 committed Apr 16, 2009
1 parent 3cc0a2f commit e3cf118
Show file tree
Hide file tree
Showing 14 changed files with 481 additions and 3 deletions.
3 changes: 3 additions & 0 deletions grails-app/conf/BootStrap.groovy
Expand Up @@ -5,6 +5,9 @@ class BootStrap {
def init = { servletContext ->
switch(GrailsUtil.environment){
case "development":
def admin = new User(login:"admin", password:"password", name:"Administrator", role:"admin")
admin.save()

def jdoe = new User(login:"jdoe", password:"password", name:"John Doe", role:"author")
def e1 = new Entry(title:"Grails 1.1 beta is out", summary:"Check out the new features")
def e2 = new Entry(title:"Just Released - Groovy 1.6 beta 2", summary:"It is looking good.")
Expand Down
52 changes: 52 additions & 0 deletions grails-app/controllers/EntryController.groovy
@@ -1,5 +1,57 @@
class EntryController {

def beforeInterceptor = [action:this.&auth, except:["index", "list", "show"]]

def auth() {
if(!session.user) {
redirect(controller:"user", action:"login")
return false
}
}

def scaffold = Entry

//scaffolded code with authorization checks
def edit = {
def entryInstance = Entry.get( params.id )

//limit editing to the original author
if( !(session.user.login == entryInstance.author.login) ){
flash.message = "Sorry, you can only edit your own entries."
redirect(action:list)
}

if(!entryInstance) {
flash.message = "Entry not found with id ${params.id}"
redirect(action:list)
}
else {
return [ entryInstance : entryInstance ]
}
}

def delete = {
def entryInstance = Entry.get( params.id )

//limit deletes to the original author
if( !(session.user.login == entryInstance.author.login) ){
flash.message = "Sorry, you can only delete your own entries."
redirect(action:list)
}

if(entryInstance) {
entryInstance.delete()
flash.message = "Entry ${params.id} deleted"
redirect(action:list)
}
else {
flash.message = "Entry not found with id ${params.id}"
redirect(action:list)
}
}




def list = {
if(!params.max) params.max = 10
Expand Down
34 changes: 34 additions & 0 deletions grails-app/controllers/UserController.groovy
@@ -0,0 +1,34 @@
class UserController {

def beforeInterceptor = [action:this.&auth, except:["login", "authenticate", "logout"]]

def auth() {
if( !(session?.user?.role == "admin") ){
flash.message = "You must be an administrator to perform that task."
redirect(controller:"user", action:"login")
return false
}
}

def scaffold = User

def login = {}

def authenticate = {
def user = User.findByLoginAndPassword(params.login, params.password)
if(user){
session.user = user
flash.message = "Hello ${user.name}!"
redirect(controller:"entry", action:"list")
}else{
flash.message = "Sorry, ${params.login}. Please try again."
redirect(action:"login")
}
}

def logout = {
flash.message = "Goodbye ${session.user.name}"
session.user = null
redirect(controller:"entry", action:"list")
}
}
6 changes: 3 additions & 3 deletions grails-app/domain/User.groovy
@@ -1,8 +1,8 @@
class User {
static constraints = {
login(unique:true, blank:false, nullable:false)
password(password:true, blank:false, nullable:false)
name(blank:false, nullable:false)
login(unique:true)
password(password:true)
name()
role(inList:["author", "admin"])
}

Expand Down
12 changes: 12 additions & 0 deletions grails-app/taglib/LoginTagLib.groovy
@@ -0,0 +1,12 @@
class LoginTagLib {

def loginControl = {
if(session.user){
out << "Hello ${session.user.name} "
out << """[${link(action:"logout", controller:"user"){"Logout"}}]"""
} else {
out << """[${link(action:"login", controller:"user"){"Login"}}]"""
}
}

}
5 changes: 5 additions & 0 deletions grails-app/views/entry/list.gsp
Expand Up @@ -13,6 +13,11 @@
<span class="menuButton"><g:link class="create" action="create">New Entry</g:link></span>
</div>
</g:if>

<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>

<div class="body">
<div class="list">
<g:each in="${entryInstanceList}" status="i" var="entryInstance">
Expand Down
4 changes: 4 additions & 0 deletions grails-app/views/layouts/_header.gsp
@@ -1,4 +1,8 @@
<div id="header">
<p><g:link class="header-main" controller="entry">Blogito</g:link></p>
<p class="header-sub">A tiny little blog</p>

<div id="loginHeader">
<g:loginControl />
</div>
</div>
74 changes: 74 additions & 0 deletions grails-app/views/user/create.gsp
@@ -0,0 +1,74 @@


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main" />
<title>Create User</title>
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLinkTo(dir:'')}">Home</a></span>
<span class="menuButton"><g:link class="list" action="list">User List</g:link></span>
</div>
<div class="body">
<h1>Create User</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<g:hasErrors bean="${userInstance}">
<div class="errors">
<g:renderErrors bean="${userInstance}" as="list" />
</div>
</g:hasErrors>
<g:form action="save" method="post" >
<div class="dialog">
<table>
<tbody>

<tr class="prop">
<td valign="top" class="name">
<label for="login">Login:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'login','errors')}">
<input type="text" id="login" name="login" value="${fieldValue(bean:userInstance,field:'login')}"/>
</td>
</tr>

<tr class="prop">
<td valign="top" class="name">
<label for="password">Password:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'password','errors')}">
<input type="password" id="password" name="password" value="${fieldValue(bean:userInstance,field:'password')}"/>
</td>
</tr>

<tr class="prop">
<td valign="top" class="name">
<label for="name">Name:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'name','errors')}">
<input type="text" id="name" name="name" value="${fieldValue(bean:userInstance,field:'name')}"/>
</td>
</tr>

<tr class="prop">
<td valign="top" class="name">
<label for="role">Role:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'role','errors')}">
<g:select id="role" name="role" from="${userInstance.constraints.role.inList}" value="${userInstance.role}" ></g:select>
</td>
</tr>

</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><input class="save" type="submit" value="Create" /></span>
</div>
</g:form>
</div>
</body>
</html>
93 changes: 93 additions & 0 deletions grails-app/views/user/edit.gsp
@@ -0,0 +1,93 @@


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main" />
<title>Edit User</title>
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLinkTo(dir:'')}">Home</a></span>
<span class="menuButton"><g:link class="list" action="list">User List</g:link></span>
<span class="menuButton"><g:link class="create" action="create">New User</g:link></span>
</div>
<div class="body">
<h1>Edit User</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<g:hasErrors bean="${userInstance}">
<div class="errors">
<g:renderErrors bean="${userInstance}" as="list" />
</div>
</g:hasErrors>
<g:form method="post" >
<input type="hidden" name="id" value="${userInstance?.id}" />
<div class="dialog">
<table>
<tbody>

<tr class="prop">
<td valign="top" class="name">
<label for="login">Login:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'login','errors')}">
<input type="text" id="login" name="login" value="${fieldValue(bean:userInstance,field:'login')}"/>
</td>
</tr>

<tr class="prop">
<td valign="top" class="name">
<label for="password">Password:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'password','errors')}">
<input type="password" id="password" name="password" value="${fieldValue(bean:userInstance,field:'password')}"/>
</td>
</tr>

<tr class="prop">
<td valign="top" class="name">
<label for="name">Name:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'name','errors')}">
<input type="text" id="name" name="name" value="${fieldValue(bean:userInstance,field:'name')}"/>
</td>
</tr>

<tr class="prop">
<td valign="top" class="name">
<label for="role">Role:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'role','errors')}">
<g:select id="role" name="role" from="${userInstance.constraints.role.inList}" value="${userInstance.role}" ></g:select>
</td>
</tr>

<tr class="prop">
<td valign="top" class="name">
<label for="entries">Entries:</label>
</td>
<td valign="top" class="value ${hasErrors(bean:userInstance,field:'entries','errors')}">

<ul>
<g:each var="e" in="${userInstance?.entries?}">
<li><g:link controller="entry" action="show" id="${e.id}">${e?.encodeAsHTML()}</g:link></li>
</g:each>
</ul>
<g:link controller="entry" params="['user.id':userInstance?.id]" action="create">Add Entry</g:link>

</td>
</tr>

</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><g:actionSubmit class="save" value="Update" /></span>
<span class="button"><g:actionSubmit class="delete" onclick="return confirm('Are you sure?');" value="Delete" /></span>
</div>
</g:form>
</div>
</body>
</html>
60 changes: 60 additions & 0 deletions grails-app/views/user/list.gsp
@@ -0,0 +1,60 @@


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main" />
<title>User List</title>
</head>
<body>
<div class="nav">
<span class="menuButton"><a class="home" href="${createLinkTo(dir:'')}">Home</a></span>
<span class="menuButton"><g:link class="create" action="create">New User</g:link></span>
</div>
<div class="body">
<h1>User List</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<div class="list">
<table>
<thead>
<tr>

<g:sortableColumn property="id" title="Id" />

<g:sortableColumn property="login" title="Login" />

<g:sortableColumn property="password" title="Password" />

<g:sortableColumn property="name" title="Name" />

<g:sortableColumn property="role" title="Role" />

</tr>
</thead>
<tbody>
<g:each in="${userInstanceList}" status="i" var="userInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">

<td><g:link action="show" id="${userInstance.id}">${fieldValue(bean:userInstance, field:'id')}</g:link></td>

<td>${fieldValue(bean:userInstance, field:'login')}</td>

<td>${fieldValue(bean:userInstance, field:'password')}</td>

<td>${fieldValue(bean:userInstance, field:'name')}</td>

<td>${fieldValue(bean:userInstance, field:'role')}</td>

</tr>
</g:each>
</tbody>
</table>
</div>
<div class="paginateButtons">
<g:paginate total="${User.count()}" />
</div>
</div>
</body>
</html>

0 comments on commit e3cf118

Please sign in to comment.