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

all(match = '') click to toggle source

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
create(attributes = {}) click to toggle source

See @create.

# File lib/reactor/cm/group.rb, line 63
def create(attributes = {})
  object = new(attributes)
  object.send(:create)
  object
end
exists?(name) click to toggle source

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
get(name) click to toggle source

See @get.

# File lib/reactor/cm/group.rb, line 56
def get(name)
  object = new(:name => name)
  object.send(:get)
  object
end
new(attributes = {}) click to toggle source
# File lib/reactor/cm/group.rb, line 208
def initialize(attributes = {})
  update_attributes(attributes)
end

Public Instance Methods

add_users!(users) click to toggle source

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
delete!() click to toggle source

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
global_permission?(name) click to toggle source

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
grant_global_permissions!(permissions) click to toggle source

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_users!(users) click to toggle source

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
rename!(name) click to toggle source

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
revoke_global_permissions!(permissions) click to toggle source

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
save!() click to toggle source

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_global_permissions!(permissions) click to toggle source

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_users!(users) click to toggle source

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
user?(name) click to toggle source

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

base_name() click to toggle source

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
create() click to toggle source

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
get() click to toggle source

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