26

I am trying to add a new role (ROLE_REPORTS) on a project generated using JHipster. I can see the tables that need to be updated (role, authority and role_authority mapping), but I am not sure how to go about the Java part of it.

There a few bits and pieces I can figure out, but I am concerned that my customisation may break some design philosophies (like the Swagger API, Spring Security, etc.,)

Has anyone already attempted it and if so any help in the right direction will be highly appreciated.

Pablo
  • 2,054
  • 8
  • 30
  • 56
user1880957
  • 1,146
  • 3
  • 15
  • 29
  • 1
    Update: Following this blog http://en.tekstenuitleg.net/blog/spring-security-with-roles-and-rights and trying to embed this into a JHipster project. Meanwhile, any help on the original question will be definitely appreciated. – user1880957 Dec 03 '14 at 10:34

5 Answers5

27

Add it to security/AuthoritiesConstants.java. and webapps/scripts/contstants.js. In the example below, an authority/role of MANAGER was added.

public final class AuthoritiesConstants {

    private AuthoritiesConstants() {
    }

    public static final String ADMIN = "ROLE_ADMIN";

    public static final String USER = "ROLE_USER";

    public static final String MANAGER = "ROLE_MANAGER";

    public static final String ANONYMOUS = "ROLE_ANONYMOUS";
}

And in constants.js:

myApp.constant('USER_ROLES', {
        'all': '*',
        'admin': 'ROLE_ADMIN',
        'user': 'ROLE_USER',
        'manager', 'ROLE_MANAGER'
    });

The new role must be added to the database. For example, the authorities.csv:

name
ROLE_ADMIN
ROLE_USER
ROLE_MANAGER
Rori Stumpf
  • 1,912
  • 19
  • 26
  • 2
    I can't find the constants.js file in the current 2. 18 version. I guess it is no longer there. – vic Jul 13 '15 at 03:51
  • 4
    What is the current workflow to add roles in Jhipster 2.20? constants.js no longer exists. – Sami Sep 07 '15 at 10:16
  • 1
    In the current version of Jhipster you do not need to update `constants.js` (that file no longer exists), but you do need to update the lists of roles that are used by Angular for user management in `user-management.controller.js` and `user-management-dialog.controller.js` – mark.monteiro Aug 09 '16 at 20:51
  • 2
    In the current 3.8.0 version of Jhipster this does not work. Here the correct answer http://stackoverflow.com/a/34890367/3937190 – Andrei Krasutski Sep 29 '16 at 08:20
3

This is will be even easier in 4.5.5

1- Modify AuthoritiesConstants.java

2- Add new role in authorities.csv file

re-Run the application, the new role should appear in the interface (Administration/user management/create a new user) (maybe it can be useful to delete target\h2db\db content in your app)

krish
  • 1,081
  • 9
  • 10
  • 1
    If you are running a production version of your site and don't want to blow away your database, then don't add the new roles in authorities.csv. It causes liquibase to throw an exception. Instead, you'll need to go into your database and add the roles manually to JHI_AUTHORITY. You still need to do step 1, though. – qanwi1970 Oct 25 '17 at 14:22
2

For Jhipter 4.3 there is this solution:

  1. Modify AuthoritiesConstants.java
  2. Add new role in authorities.csv file
  3. Update the table JHI_AUTHORITY by including the new role
  4. Modify user-management-dialog-component.ts located here \webapp\app\admin\user-management

Re-run the application. The new role should appear in the interface (Administration/user management/create a new user). (Maybe it can be useful to delete target\h2db\db content in your app.)

cokeman19
  • 2,405
  • 1
  • 25
  • 40
Yannard
  • 69
  • 1
  • 4
1

Adding the values to the authorities.csv wouldnt add those values to the tables directly ,as this is just a value added to table,so there is not differnce in the Liquibase change we will have to add it manually to DB...

faizal khan
  • 105
  • 1
  • 8
0

After editing security/AuthoritiesConstants.java. You can add a new liquibase changelog and add a new app_authorities.csv. As below

Image showing how to do

My app_authorities.csv looks like this.

name
ROLE_VENDOR
Johan
  • 3,667
  • 6
  • 20
  • 25
Joe King
  • 11
  • 1