Adding security to the CRUD module

The CRUD module is the base of any rapid prototyping module. It helps you to administer data in the backend, while still being quick to create a frontend that closely resembles your prototype. For example, when creating an online shop, your first task is to create a nice looking frontend. Still it would be useful if you could change some things such as product name, description, or ID in the backend. This is where CRUD helps. However, there is no security inside the CRUD module, so anyone can add or delete data. This is where the secure module can help.

You can find the source code of this example in the chapter3/crud-secure directory.

Getting ready

You should already have added controllers for CRUD editing, as well as an infrastructure for authentication or at least something similar to a user entity. If you do not know about this part, you can read about it at http://www.playframework.org/documentation/1.2/crud.

How to do it...

You should have your own security implementation, something like this:

public class Security extends Secure.Security {

    static boolean authenticate(String username, String password) {
        User user = User.find("byUserAndPassword", username, Crypto.passwordHash(password)).first();
        return user != null;
    }

    static boolean check(String profile) {
        if ("admin".equals(profile)) {
          User user = User.find("byUser", connected()).first();
          if (user != null) {
            return user.isAdmin;
          }
        } else if ("user".equals(profile)) {
          return connected().equals("user");
        }
        return false;
    }
}

Adding users via CRUD should only be done by the admin:

@Check("admin")
@With(Secure.class)
public class Users extends CRUD {
}

However, creating Merchants should never be allowed for the admin, but only by an authorized user. Deleting (most data on live systems will not be deleted anyway) Merchants, however, should be an admin only task again:

@With(Secure.class)
public class Merchants extends CRUD {

    @Check("admin")
    public static void delete(String id) {
        CRUD.delete(id);
    }

    @Check("user")
    public static void create() throws Exception {
        CRUD.create();
    }
}

How it works...

As you can see, you can easily secure complete controllers to be only accessible for logged-in users. Furthermore you can also make only special controllers available for certain users. As these methods are static, you are not able to call super() in them, but need to define the static methods of the parent controller again and then manually call the methods of the CRUD controller.

There's more...

CRUD should never be a big topic in your finished business application because your business logic will be far more complex than adding or removing entities. However, it can be a base for certain tasks. This is where more advanced aspects come in handy.

Changing the design of the CRUD user interface

You can use the play crud:ov --template Foo/bar command line call to copy the template HTML code to Foo/bar.html, so you can edit it and adapt it to your corporate design.

Checking out the scaffold module

There is also the scaffold module you can take a look at. It generates controllers and templates by inferring the information of your model classes when you run play scaffold:gen on the command line. It currently works for JPA and Siena.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.237.79