~peter-kovac11/play/play-scala-console-improvements

« back to all changes in this revision

Viewing changes to samples-and-tests/yabe-with-scala/app/controllers/Admin.scala

  • Committer: guillaume
  • Date: 2009-11-11 17:19:54 UTC
  • Revision ID: guillaume@macbook-pro-de-guillaume.local-20091111171954-1z761dqr0aw93747
yabe-with-scala

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package controllers
 
2
 
 
3
import play._
 
4
import play.Scala._
 
5
import play.mvc._
 
6
import play.data.validation._
 
7
import play.db.jpa.QueryFunctions._
 
8
 
 
9
import models._
 
10
 
 
11
@With(Array(classOf[Secure])) 
 
12
object Admin extends Actions with Defaults {
 
13
    
 
14
    @Before
 
15
    private def setConnectedUser{
 
16
        if(Secure.Security.isConnected()) {
 
17
            val user = find[User]("byEmail", Secure.Security.connected()).first
 
18
            renderArgs += "user" -> user.fullname
 
19
        }
 
20
    }
 
21
 
 
22
    def index {
 
23
        val posts = find[Post]("author.email", Secure.Security.connected()).fetch
 
24
        render(posts)
 
25
    }
 
26
    
 
27
    def form(id: Long) {
 
28
        if(id != 0) {
 
29
            val post = findById[Post](id)
 
30
            render(post)
 
31
        }
 
32
        render()
 
33
    }
 
34
    
 
35
    def save(id: Long, title: String, content: String, tags: String) {
 
36
        var post: Post = null
 
37
        if(id == 0) {
 
38
            // Create post
 
39
            val author = find[User]("byEmail", Secure.Security.connected()).first;
 
40
            post = new Post(author, title, content)
 
41
        } else {
 
42
            // Retrieve post
 
43
            post = findById[Post](id)
 
44
            post.title = title
 
45
            post.content = content
 
46
            post.tags.clear()
 
47
        }
 
48
        // Set tags list
 
49
        tags.split("""\s+""") foreach { tag: String =>
 
50
            if(tag.trim().length > 0) {
 
51
                post.tags add Tag.findOrCreateByName(tag)
 
52
            }
 
53
        }
 
54
        // Validate
 
55
        validation.valid(post)
 
56
        if(Validation.hasErrors()) {
 
57
            render("@form", post)
 
58
        }
 
59
        // Save
 
60
        post.save()
 
61
        index
 
62
    }
 
63
    
 
64
}
 
65
 
 
66
// Security
 
67
 
 
68
object Security extends Secure.Security {
 
69
 
 
70
    private def authentify(username: String, password: String) = {
 
71
        User.connect(username, password) != null
 
72
    }
 
73
    
 
74
    private def check(profile: String) = {
 
75
        profile match {
 
76
            case "admin" => find[User]("byEmail", Secure.Security.connected).first.isAdmin
 
77
            case _ => false
 
78
        }
 
79
    }
 
80
    
 
81
    private def onDisconnected = Application.index
 
82
    
 
83
    private def onAuthenticated = Admin.index
 
84
    
 
85
}
 
86
 
 
87
// CRUD
 
88
 
 
89
@Check(Array("admin")) @With(Array(classOf[Secure])) object Comments extends CRUD
 
90
@Check(Array("admin")) @With(Array(classOf[Secure])) object Posts extends CRUD 
 
91
@Check(Array("admin")) @With(Array(classOf[Secure])) object Tags extends CRUD
 
92
@Check(Array("admin")) @With(Array(classOf[Secure])) object Users extends CRUD 
 
93