Set Customer-Role via rails console

Infos:

  • Used Zammad version: 3.3.x
  • Used Zammad installation source: (source, package, …) deb
  • Operating system: ubuntu 18.04

Is there someone who can give me a command for setting the customer-role to every user who is not already in at least one role via api or rails?
Reason why I’m asking for this: We disabled the Customer-Role for a longer time. But side-affect of this is, that only admins are able to edit customers. As we now planning to let agents also edit customers, I now need to re-enable the role and assign our customers to it.

Greetings,

daniel

Users without assigned role technically don’t break anything.

Use the below with absolute care. It has not been tested thorougly by me and potentially may set wrong permissions which may led to a security issue. Test in a test environment before if you can. If you run the command, you may rather want to run it if your system is not busy, because the script below is super expensive in I/O with many users.

Danger zone

I expect you to understand the script and know what you’re doing if you run it. I will not fix your system if it breaks because of this script.

# Ensure to define all Roles NOT AFFECTED in below list
leroles = Role.where(name: ['Admin','Agent']).ids
# Define role the user shall have after wards
destrole = Role.where(name: ['Customer'])

# Below loops through ALL users and ignores user#1 (system) and all
# users with earlier set roles.
feedback = ''
User.all.each{|user|
  next if user.id == 1
  next if user.roles.ids == leroles
  feedback += "Touching user #{user.id} ...\n"
  feedback += "... Roles before touching: #{user.roles.ids}\n"
  user.roles = destrole
  user.save
}
# Return all affected users for reference
puts feedback

Thanks a lot!

If I understand your “Online-Liner ( :wink: )” right, it ignores only users with ID 1 and if user is in Group Admin and Agent. Is there an Option for ignoring every user where any role is set?

Possibly, but that’s out of my time scope - sorry.