7

I tried to add and change roles in jhipster. First I just tried to change one use case's role to admin from user. Then I tested it and user can add employee even if the roles is ROLE_ADMIN so it didn't change anything.

I added new role as well, called MANAGER. I edited AuthoritiesConstants.java and added new role to JHI_AUTHORITY-table. Should I do something else or is this enough to get this working?

state('employee.new', {
            parent: 'employee',
            url: '/new',
            data: {
                roles: ['ROLE_ADMIN'],
            },
            onEnter: ['$stateParams', '$state', '$modal', function($stateParams, $state, $modal) {
                $modal.open({
                    templateUrl: 'scripts/app/entities/employee/employee-dialog.html',
                    controller: 'EmployeeDialogController',
                    size: 'lg',
                    resolve: {
                        entity: function () {
                            return {nameFirst: null, nameLast: null, taxNumber: null, isFinnish: null, finnishSOTU: null, valtticard: null, birthDate: null, isContactPerson: null, isTiedonantaja: null, cOTARKENNE: null, id: null};
                        }
                    }
                }).result.then(function(result) {
                    $state.go('employee', null, { reload: true });
                }, function() {
                    $state.go('employee');
                })
            }]
        })
Sami
  • 2,311
  • 13
  • 46
  • 80
  • I'm having the same issue. Role assigned to state does not restrict access to page when logged in with a different role. – Pedro Madrid Oct 22 '15 at 16:21

6 Answers6

13

Edit the following 6 files to include/exclude code specified in blocks to add/remove a role(ROLE_MANAGER as an example)

  1. AuthoritiesConstants.java (constant to be used in java)

    public static final String MANAGER = "ROLE_MANAGER";

  2. src/main/resources/config/liquibase/authorities.csv (proper liquidbase update)

    ROLE_MANAGER

  3. src/main/resources/config/liquibase/users.csv (add username: manager with password: user)

    5;manager;$2a$10$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K;Manager;Manager;manager@localhost;true;en;system

  4. src/main/resources/config/liquibase/users_authorities.csv (another proper liquidbase update)

    5;ROLE_MANAGER

  5. src/main/webapp/app/admin/user-management/user-management.controller.js (for role to be available in JavaScript)

    $scope.authorities = ["ROLE_USER", "ROLE_ADMIN", "ROLE_MANAGER"];

  6. src/main/webapp/app/admin/user-management/user-management-dialog.controller.js (for role to be available in JavaScript)

    $scope.authorities = ["ROLE_USER", "ROLE_ADMIN", "ROLE_MANAGER"];

Restart the server once everything is in place and double check JHI_AUTHORITY and JHI_USER_AUTHORITY tables after application launch for a new ROLE_MANAGER to be there. Login into system with username: 'manager' and password: 'user'.

Community
  • 1
  • 1
  • 2
    UnexpectedLiquibaseException: CSV Line 5 has only 9 columns, the header has 10. I corrected as follows. Edited users.csv: 5;manager;$2a$10$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K;Manager;Manager;manager@localhost;true;en;system;system – Andrei Krasutski Sep 29 '16 at 08:15
  • I had to drop the database and re-run the server to make it work. As otherwise liquidbase was complaining about checksum validation error; `Liquibase could not start correctly, your database is NOT ready: Validation Failed` – khawarizmi Nov 11 '16 at 11:32
  • 2
    src/main/webapp/app/admin/user-management/user-management.controller.js is not available anymore – netshark1000 May 14 '17 at 10:54
1

You must insert new role into JHI_AUTHORITY table then grant this role to some users in JHI_USER_AUTHORITY table. This means updating authorities.csv and users_authorities.csv file if you re-create your database (e.g. if you use H2).

On client-side, just add new role to roles property of your state definitions.

Gaël Marziou
  • 16,028
  • 4
  • 38
  • 49
  • But if I add roles: ['ROLE_ADMIN'], does it mean that ROLE_USER can't go to employee.new? In my case it didn't work that way, User could add a new employee as well, so something is missing now? I am using mySQL and I added new role by hand to db. – Sami Sep 08 '15 at 20:07
  • Roles are independent from each others, a user having ROLE_ADMIN does not necessary have ROLE_USER, it's up to you to grant both roles to same user. In your state you can set roles: ['ROLE_ADMIN', 'ROLE_USER'] if you want to authorize user with any of these 2 roles to access this state. – Gaël Marziou Sep 08 '15 at 20:56
  • I mean that I have roles: ['ROLE_ADMIN'], but still user with role ROLE_USER could go and add new employees, the roles are not working in my case. – Sami Sep 08 '15 at 21:23
  • Are you sure that your API returns correct roles, have you checked in browser's console? – Gaël Marziou Sep 09 '15 at 08:58
  • roles: ["ROLE_USER"] 0: "ROLE_USER" This in response when log in. – Sami Sep 09 '15 at 20:35
1

I have found an easiest way:

  1. Disable liquibase from .gradle file (in my case App>gradle>profile_dev.gradle) by changing the following:

    def profiles = 'dev,no-liquibase' //if (project.hasProperty('no-liquibase')) { // profiles += ',no-liquibase' //}

  2. Now change in src/main/webapp/scripts/app/admin/user-management/user-management.controller.js to add your role.

    $scope.authorities = ["ROLE_USER", "ROLE_ADMIN", "YOUR_ROLE"];

  3. And src/main/webapp/scripts/app/admin/user-management/user-management-dialog.controller.js

    $scope.authorities = ["ROLE_USER", "ROLE_ADMIN", "YOUR_ROLE"];

  4. Finally add "YOUR_ROLE" in "name" column of "jhi_authority" table in database and save. Now restart application and you will able to create user with your newly created role.

Sakib
  • 85
  • 2
  • 11
1

Taking a leaf out of @Constantin Zagorsky here are the steps that work.

  1. AuthoritiesConstants.java (constant to be used in java)

public static final String MANAGER = "ROLE_MANAGER";

2.src/main/resources/config/liquibase/authorities.csv (proper liquibase update) [This will not run. But important to keep in sync with DB]

ROLE_MANAGER

  1. Update DB [Important because liquibase will not pick up changes made in authorities,csv in step 2]

    insert into jhi_authority values ('ROLE_MANAGER');

  2. src/main/webapp/app/admin/user-management/user-management.controller.js(for role to be available in JavaScript)

$scope.authorities = ["ROLE_USER", "ROLE_ADMIN", "ROLE_MANAGER"];

  1. src/main/webapp/app/admin/user-management/user-management-dialog.controller.js(for role to be available in JavaScript)

$scope.authorities = ["ROLE_USER", "ROLE_ADMIN", "ROLE_MANAGER"];

  1. Modify public User createUser(ManagedUserVM managedUserVM) method in UserService.java (Very Important). Modify default password generation logic

// comment default password generation. In my case I made the default //user as same as userid

//String encryptedPassword = passwordEncoder.encode(RandomUtil.generatePassword()); String encryptedPassword = passwordEncoder.encode(managedUserVM.getLogin());

  1. Log into application as Admin
  2. Add new user with a new role. Default password would be same as username.
Ice
  • 1,783
  • 4
  • 26
  • 52
Abhijit Mazumder
  • 8,641
  • 7
  • 36
  • 44
0

After above instruction I got:

ERROR [...]f.config.liquibase.AsyncSpringLiquibase  : Liquibase could not start correctly, your database is NOT ready: Validation Failed:
[...]

To avoid that, run

./mvnw liquibase:clearCheckSums

User with new role, has no access to account settings, so you have to add new roles to

/src/main/webapp/app/account/password/password.state.js
/src/main/webapp/app/account/sessions/sessions.state.js
/src/main/webapp/app/account/settings/settings.state.js

Complete instruction: https://codefitter2.blogspot.com/2016/11/how-to-create-new-role-in-jhipster.html

Nico
  • 505
  • 4
  • 14
0

In JHipster 6, the csv file is named authority.csv. So change this file along the AuthoritiesConstants.java as described in previous posts.

enter image description here

Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74