Play Framework full implementation of one app (11)

Source: Internet
Author: User

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> &lt 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.