class Reactor::Cm::Group
The Group
class can be used to work with user groups defined or known to the content manager. It allows you to create, edit and delete groups, handle users and permissions and get the group meta data. The Group
class does not respect the user management defined under “config/userManagement.xml”, but is the basis for class like @EditorialGroup or @LiveGroup that respect the user management.
Public Class Methods
Returns all known group names as an array of strings.
# File lib/reactor/cm/group.rb, line 34 def all(match = '') object = new base_name = object.send(:base_name) request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, 'groupText', match) xml.get_key_tag!(base_name, 'name') end begin response = request.execute! groups = ResponseHandler::String.new.get(response, '//group/name/text()') groups.is_a?(Array) ? groups : [groups] rescue XmlRequestError [] end end
See @create.
# File lib/reactor/cm/group.rb, line 63 def create(attributes = {}) object = new(attributes) object.send(:create) object end
Method returns true if a group with the given name
exists, false otherwise.
# File lib/reactor/cm/group.rb, line 23 def exists?(name) object = new(:name => name) begin object.send(:get).present? rescue XmlRequestError false end end
See @get.
# File lib/reactor/cm/group.rb, line 56 def get(name) object = new(:name => name) object.send(:get) object end
# File lib/reactor/cm/group.rb, line 208 def initialize(attributes = {}) update_attributes(attributes) end
Public Instance Methods
Add the given users
to the current set of group users.
# File lib/reactor/cm/group.rb, line 119 def add_users!(users) users = users.kind_of?(Array) ? users : [users] users = self.users | users set_users(users) end
Deletes the current group instance.
# File lib/reactor/cm/group.rb, line 165 def delete! request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, self.class.primary_key, self.name) xml.delete_tag!(base_name) end response = request.execute! response.ok? end
Returns true, if a global permission with the given name
exists, false otherwise.
# File lib/reactor/cm/group.rb, line 81 def global_permission?(name) self.global_permissions.include?(name.to_s) end
Add the given permissions
to the current set of group permissions.
# File lib/reactor/cm/group.rb, line 86 def grant_global_permissions!(permissions) permissions = permissions.kind_of?(Array) ? permissions : [permissions] permissions = self.global_permissions | permissions set_global_permissions!(permissions) end
Remove the given users
from the current set of group users.
# File lib/reactor/cm/group.rb, line 127 def remove_users!(users) users = users.kind_of?(Array) ? users : [users] users = self.users - users set_users(users) end
As it is not possible to actually rename an existing group, this method creates a new group with the same attributes but a different name as the current instance and deletes the old group. The method returns the new group object.
# File lib/reactor/cm/group.rb, line 179 def rename!(name) new_attributes = self.class.attributes.inject({}) do |hash, mapping| key, _ = mapping hash[key] = self.send(key) hash end if self.delete! new_attributes[:name] = name self.class.create(new_attributes) else false end end
Take away the given permissions
from the current set of group permissions.
# File lib/reactor/cm/group.rb, line 94 def revoke_global_permissions!(permissions) permissions = permissions.kind_of?(Array) ? permissions : [permissions] permissions = self.global_permissions - permissions set_global_permissions(permissions) end
Saves all settable instance attributes to the Content Manager.
# File lib/reactor/cm/group.rb, line 147 def save! request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, self.class.primary_key, self.name) xml.set_tag!(base_name) do self.class.attributes(:set).each do |name, xml_attribute| value = self.send(name) xml.value_tag!(xml_attribute.name, value) end end end response = request.execute! response.ok? end
Set the group permissions to the given permissions
.
# File lib/reactor/cm/group.rb, line 102 def set_global_permissions!(permissions) request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, self.class.primary_key, self.name) xml.set_key_tag!(base_name, self.class.xml_attribute(:global_permissions).name, permissions) end request.execute! self.global_permissions = permissions end
Set the group users to the given users
.
# File lib/reactor/cm/group.rb, line 135 def set_users!(users) request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, self.class.primary_key, self.name) xml.set_key_tag!(base_name, self.class.xml_attribute(:users).name, users) end request.execute! self.users = users end
Returns true, if an user with the given name
exists, false otherwise.
# File lib/reactor/cm/group.rb, line 114 def user?(name) users.include?(name) end
Protected Instance Methods
The group base name can either be “group”, “groupProxy”, or “secondaryGroupProxy”. Only the two proxy group names take the configured user management (config/userManagement.xml) into account. Use EditorialGroup
to work on editorial groups and LiveGroup
to work on live groups.
# File lib/reactor/cm/group.rb, line 204 def base_name 'group' end
Creates a new group and sets all attributes that are settable on create. Other attributes are ignored and would be overwritten by the final get
call.
# File lib/reactor/cm/group.rb, line 232 def create request = XmlRequest.prepare do |xml| xml.create_tag!(base_name) do |xml| self.class.attributes(:create).each do |name, xml_attribute| value = self.send(name) xml.value_tag!(xml_attribute.name, value) if value.present? end end end response = request.execute! self.name = self.class.response_handler.get(response, self.class.xml_attribute(:name)) get end
Retrieves a single group matching the name set in the current instance.
# File lib/reactor/cm/group.rb, line 213 def get request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, self.class.primary_key, self.name) xml.get_key_tag!(base_name, self.class.xml_attribute_names) end response = request.execute! self.class.attributes(:get).each do |name, xml_attribute| value = self.class.response_handler.get(response, xml_attribute) set_attribute(name, value) end self end