Tuesday, May 12, 2015

Automatically add new created users (by email or ldap login) to a project in Redmine

Alright, just got this to work a couple minutes ago:

* I'm using the Redmine's rake to create issues by fetching email messages (read this):

RAILS_ENV="production" /usr/bin/rake redmine:email:receive_imap host=imap.gmail.com port=993 ssl=1 username=myemailusername@mydomain.com password=myPassword project=helpdesk unknown_user=create no_permission_check=1 no_account_notice=1

Project id: helpdesk
Default user role: Customer

* If the email address of the sender is not in Redmine's db, it will create a new user with that email. Let call it joeuser.

The problem is the new created user was not added to the project ('helpdesk'), so she will not receive any follow-up email messages (any updates of the ticket).

So, I have to modify the Redmine's core to add the new user to the helpdesk project with the Customer role:

1. New user created by the Email task:

/path/to/redmine/app/models/mail_handler.rb

     if addr && !addr.spec.blank?
       user = self.class.new_user_from_attributes(addr.spec, TMail::Unquoter.unquote_and_convert_to(addr.name, 'utf-8'))
       if user.save
         member = Member.new
         member.user = user
         member.project = Project.find_by_identifier('helpdesk')
         member.roles = [Role.find_by_name('Customer')]
         member.save
         user
       else
         logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
2. New user created by LDAP login:

/path/to/redmine/app/models/user.rb

         if user.save
           user.reload
           member = Member.new
           member.user = user
           member.project = Project.find_by_identifier('helpdesk')
           member.roles = [Role.find_by_name('Customer')]
           member.save
           logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
         end
       end

3. Restart Apache or nginx:

$ sudo service apache2 restart


And the world is better now!!!