List of users by role

Added by Grant Johnson almost 10 years ago

Hi,
How can I get a list of users by roles?

Replies (14)

RE: List of users by role - Added by Felix Schäfer almost 10 years ago

For a project: on the project overview page.

RE: List of users by role - Added by Grant Johnson almost 10 years ago

Thanks... yes I can get that.

I need it across projects (I have about 200+)

RE: List of users by role - Added by Felix Schäfer almost 10 years ago

Other than on the CLI, not possible.

RE: List of users by role - Added by Grant Johnson almost 10 years ago

Thanks Felix. I'll hit the command line!

RE: List of users by role - Added by Felix Schäfer almost 10 years ago

If you need it as a standalone script, put this in a single file:

#!/usr/bin/env /path/to/redmine/script/runner

Project.all.each {|p| puts "#{p}:"; p.users_by_role.sort.each {|r| puts "  #{r[0]}:"; r[1].each {|u| puts "    #{u}"}}}

Make the file executable and call it with RAILS_ENV="prodcution" ./list_of_users_by_role, it will output something like that:

Project 1:
  Role 1:
    User 1
    User 2
  Role 2:
    User 3
    User 1
Project 2:
  Role 2:
    User 1
    USer 2
  Role 4:
    User 1

RE: List of users by role - Added by Grant Johnson almost 10 years ago

That works great... Thanks.

But I've been told that I need firstname, lastname, email and role only.

"Jimmy, Jones, , Manager_role"

We don't even need it by project....

I don't follow the Ruby script... Can you help?
:-)

RE: List of users by role - Added by Felix Schäfer almost 10 years ago

Well, users can have multiple roles, first in different projects, and also different roles in the same project. What "role" do you need there?

RE: List of users by role - Added by Grant Johnson almost 10 years ago

Felix,

duh... of course you're right.
How about just returning email instead of name in the first script?

Where can I find the docs/parameters to rite these scripts?

RE: List of users by role - Added by Felix Schäfer almost 10 years ago

#!/usr/bin/env /path/to/redmine/script/runner

Project.all.each {|p| puts "#{p}:"; p.users_by_role.sort.each {|r| puts "  #{r[0]}:"; r[1].each {|u| puts "    #{u.mail}"}}}

This will output only the mails. If you need another format, just change the last string " #{u.mail}" to whatever you need. Short explanation on that: " " is a string, #{ } in a string interpolates the ruby code inside to a string, u in that context is the user object you are currently looking at. " #{u}" in the above example gives 4 blanks followed by the name of the user, because the interpolator #{ } in essence calls to_s on the object it receives to force it to a String, in the case of the User object, to_s is aliased to name, which itself returns the name of the user formatted according to the global settings in redmine.

As to the script itself: it's essentially a ruby script run in the "redmine environment", so to learn more about them, get up to speed on ruby, rails and redmine code (at least the models) :-)

RE: List of users by role - Added by Pavel Potcheptsov about 7 years ago

How to do this with current redmine?
I try do this in such way but with no success:

[root@redmine ~]# cat userperroles 
cd /var/www/rm2.3.0 && \
/usr/local/rvm/rubies/ruby-1.9.3-p327/bin/ruby \
/var/www/rm2.3.0/script/rails runner "Project.all.each {|p| puts "#{p}:"; p.users_by_role.sort.each {|r| puts "  #{r[0]}:"; r[1].each {|u| puts "    #{u}"}}}" -e production
[root@redmine ~]# RAILS_ENV="production" ./userperroles > userperroles.db
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/runner.rb:53:in `eval': (eval):1: syntax error, unexpected $end, expecting '}' (SyntaxError)
Project.all.each {|p| puts #{p}:; p.users_by_role.sort.each {|r| puts 
                                                                      ^
        from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands/runner.rb:53:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands.rb:64:in `require'
        from /usr/local/rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.13/lib/rails/commands.rb:64:in `<top (required)>'
        from /var/www/rm2.3.0/script/rails:6:in `require'
        from /var/www/rm2.3.0/script/rails:6:in `<main>'

RE: List of users by role - Added by Kuniharu AKAHANE about 7 years ago

Hi, Pavel

It still work with current redmine (2.3-stable).
There are 2 quoting characters should be replaced.

1) runner "Project.all   ->  runner 'Project.all
2) }" -e production   ->   }' -e production
  • Whole script Diff
    --- userperroles_with_error    2013-07-23 20:32:44.000000000 +0900
    +++ userperroles    2013-07-23 20:32:56.000000000 +0900
    @@ -1,3 +1,3 @@
     cd /Users/kakahane/projects/redmine/redmine-2.3-stable && \
     /Users/kakahane/.rbenv/shims/ruby \
    -/Users/kakahane/projects/redmine/redmine-2.3-stable/script/rails runner "Project.all.each {|p| puts "#{p}:"; p.users_by_role.sort.each {|r| puts "  #{r[0]}:"; r[1].each {|u| puts "    #{u}"}}}" -e production
    +/Users/kakahane/projects/redmine/redmine-2.3-stable/script/rails runner 'Project.all.each {|p| puts "#{p}:"; p.users_by_role.sort.each {|r| puts "  #{r[0]}:"; r[1].each {|u| puts "    #{u}"}}}' -e production

Hope this help. :)

RE: List of users by role - Added by Pavel Potcheptsov about 7 years ago

Thank you very much, while you didn't answer I have found another way:

[root@redmine rm2.3.0]# ruby ./script/rails console production
Loading production environment (Rails 3.2.13)
1.9.3-p327 :001 > ActiveRecord::Base.logger.level = 1
 => 1 
1.9.3-p327 :002 > $stdout = File.new('/root/console.out', 'w')
1.9.3-p327 :003 > $stdout.sync = true
1.9.3-p327 :004 > Project.all.each {|p| puts "#{p}:"; p.users_by_role.sort.each {|r| puts "  #{r[0]}:"; r[1].each {|u| puts "    #{u}"}}}
1.9.3-p327 :005 > exit

But now I have tried you suggestion and it works!
Can you suggest solution how to modify this query to get similar list but with specified type of roles, i.e. I want to see which users belong to roles Role1, Role2?

RE: List of users by role - Added by Pavel Potcheptsov about 7 years ago

I also couldn't find relation between users and roles in Mysql.
There are:
- users table with id field,
- roles table with id field,
- member_roles table with member_id and role_id fields, but member_id doesn't equal users.id
For example I need to remove one Role. I found all users that belong to this Role with above task and with text searching, but I can't remove this role due some users still assigned to it and this users are archived.
So I need to find users with users.status=3 and then find which of them belong to specified member_roles.role_id.

RE: List of users by role - Added by François Bélingard almost 7 years ago

It would be great if we can do this directly in the admin panel. What do you think ?

(1-14/14)