Add Permission Control
1. Import the Secure module, which provides a controllers. Secure Controller.
/conf/application.conf
# Import the Secure modulemodule.secure=${play.path}/modules/secure
/conf/routes
# Import Secure routes* / module:secure
2. Add a label on the post Comment User tag Controller
@With (Secure. Class)publicclassextends CRUD { }
Start server, Access Localhost:9000/admin, if the page error (or compile failed), refer to the previous Chapter add CRUD module
3. Add Permission Control
Create a rights controller /controllers/security.java
import models.*; Public class extends secure.security { staticboolean Authenticate (string username, string password) { return User.connect (username, password)! = NULL; }}
Log in with the correct user name password by localhost:9000/logout log out
4. Add Login Page
Create admin Controller
@With (Secure. Class ) public class Admin extends Controller {@Before static void Setconnecteduser () { if ( Security.isconnected ()) {User User = User.find ("Byemail" , Security.connec Ted ()). First (); Renderargs.put ( "user" public static void index () {render (); }}
Add route
# administrationget /admin/? admin.index* /admin Module:crud
Modify Main.html
<ul id= "Tools" > <li> <a href= "@{admin.index ()}" >log in to write Something</a> </li></ul>
Add /views/admin.html
<! DOCTYPE html>
Add /views/admin/index.html
#{extends ' admin.html '/} Welcome ${user}!
5. Add a role
Security controller Override check method
Static Boolean Check (String profile) { if(' admin '. Equals (Profile)} { return user.find ("Byemail", Connected ()) .<user> first(). ISAdmin; } return false ;}
Modify admin.html to show whether the user has the Admin role
<div id= "main" > <ul id= "Adminmenu" > <li class= "${request.controller = = ' Admin '?" ' Selected ': '} ' > <a href= ' @{admin.index ()} ' >my posts</a> </li> #{secure.ch Eck ' admin '} <li class= "${request.controller = = ' Posts '? ' Selected ': '} ' > <a href= ' @{posts.list ()} ' >Posts</a> </li> < Li class= "${request.controller = = ' Tags '?" ' Selected ': '} ' > <a href= ' @{tags.list ()} ' >Tags</a> </li> <l I class= "${request.controller = = ' Comments '? ' Selected ': '} ' > <a href= ' @{comments.list ()} ' >Comments</a> </li> <li class= "${request.controller = = ' Users '? ' Selected ': '} ' > <a href= ' @{users.list ()} ' >Users</a> </li> #{/secu Re.check} </ul> #{dolayout/} </div>
Add tag on post, tag, user, comment controller, only User of admin role can access http://localhost:9000/admin/{####}
@Check ("admin")@With (Secure. class )publicclassextends CRUD {}
。。
Play Framework full implementation of one app (11)