Project

General

Profile

Adding groups to Projects in Redmine (with a plugin)

Added by Carlo Ruggiero over 11 years ago

i'm working on this script > http://www.redmine.org/plugins/default_members < by Sajin Andrei

I've completely modified the hook to suite my needs, but i realized that it actually adds every single user that is member of the defined group.

what i want to do is, instead, add the entire group to the project, so that if i add anyone else to the same group i'll not have to update every project.

this is my code:

# Debuggin [Default: commented/disabled]
#require 'logger'

class DefmembersHook < Redmine::Hook::ViewListener
  def controller_projects_new_after_save(context={ })
    #log = Logger.new('/usr/local/share/redmine/log/plugin.log')
    params = context[:params]
    project = context[:project]
    roles = Role.find_all_givable
    setting_group = Setting.plugin_redmine_default_members[:group] ? Setting.plugin_redmine_default_members[:group] : 'Manager'
    groups ||= setting_group.split(",")
    groups.each do |gp|
      #log.error "gp: #{gp}" 
      group = Group.find(:first, :conditions => ["LOWER(lastname) = ?", gp.to_s.downcase])
      #log.error "group: #{group}" 
      users = User.active.in_group(group).all(:limit => 100)
      users.each do |user|
        if user[:lastname] != 'Admin' && user[:lastname] != 'Anonymous'
          #log.error "inizio per #{user}" 
          rs = Role.find_by_name(group.to_s)
          #log.error "rs: #{rs}" 
          m = Member.new(:user => user, :roles => [rs])
          project.members << m
          #log.error "fine per #{user}" 
        end
      end
    end
  end
end

and it is actually working as intended by Sajin Andrei, adding single users from a group.

i want to do something like this

m = Member.new(:group => group, :roles => [rs])
project.members << m

but it doesn't work (obviously)... hope someone can help

Replies (3)

RE: Adding groups to Projects in Redmine (with a plugin) - Added by Jan Niggemann (redmine.org team member) over 11 years ago

Perhaps I'm getting it wrong, but you can do this using redmine, no need for a plugin?
Just create a group and add it to a project.

RE: Adding groups to Projects in Redmine (with a plugin) - Added by Carlo Ruggiero over 11 years ago

I'm willing to create a plugin to automatically add Groups to Projects when they are created (and assign the right role to the group)
so that i don't have to add them manually.

What i've done so far is, using an old plugin, adding members of groups to the project with the defined role.

The problem of this solution is that when i will add someone to the desired group then i'll have to add the same member to all the other projects that i've created before that the memeber was added to the group.

So the point is:
Is there a way (modifying the script i've written on OT) to add entire groups (and roles) to a project, instead of members?

RE: Adding groups to Projects in Redmine (with a plugin) - Added by Carlo Ruggiero over 11 years ago

Sorry for double posting but i found the solution...

# Modificata da CARLO RUGGIERO per RETINA
# Permette di aggiungere interi gruppi ad un progetto in automatico!
class DefmembersHook < Redmine::Hook::ViewListener
  def controller_projects_new_after_save(context={ })
    params = context[:params]
    project = context[:project]
    #roles = Role.find_all_givable
    setting_group = Setting.plugin_redmine_default_members[:group] ? Setting.plugin_redmine_default_members[:group] : 'Manager'
    groups ||= setting_group.split(",")
    groups.each do |gp|
      group = Group.find(:first, :conditions => ["LOWER(lastname) = ?", gp.to_s.downcase])
      rs = Role.find_by_name(group.to_s) 
      project.members << Member.new(:principal => group, :roles => [rs])
    end
  end
end

actually it was really simple, instead of using the :user tag i need to use the :principal tag and link to it the group.
it is working very good now, adding groups to the project :)

    (1-3/3)