Class: Reactor::Cm::Group

Inherits:
Object
  • Object
show all
Extended by:
WhereQuery
Includes:
Permissions, XmlAttributes
Defined in:
lib/reactor/cm/group.rb

Overview

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.

Direct Known Subclasses

EditorialGroup, LiveGroup

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WhereQuery

where

Methods included from Permissions

#global_permission?, #grant_global_permissions!, included, #revoke_global_permissions!, #set_global_permissions!

Class Method Details

.all(match = nil) ⇒ Object

Returns all known group names as an array of strings.



34
35
36
# File 'lib/reactor/cm/group.rb', line 34

def all(match = nil)
  where("groupText", match)
end

.base_nameObject



156
157
158
# File 'lib/reactor/cm/group.rb', line 156

def self.base_name
  "group"
end

.create(attributes = {}) ⇒ Object

See @create.



46
47
48
49
50
# File 'lib/reactor/cm/group.rb', line 46

def create(attributes = {})
  object = new(attributes)
  object.send(:create)
  object
end

.exists?(name) ⇒ Boolean

Method returns true if a group with the given name exists, false otherwise.

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# 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) ⇒ Object

See @get.



39
40
41
42
43
# File 'lib/reactor/cm/group.rb', line 39

def get(name)
  object = new(name: name)
  object.send(:get)
  object
end

Instance Method Details

#add_users!(users) ⇒ Object

Add the given users to the current set of group users.



69
70
71
72
73
74
# File 'lib/reactor/cm/group.rb', line 69

def add_users!(users)
  users = users.is_a?(Array) ? users : [users]
  users = self.users | users

  set_users(users)
end

#delete!Object

Deletes the current group instance.



115
116
117
118
119
120
121
122
123
124
# File 'lib/reactor/cm/group.rb', line 115

def delete!
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, self.class.primary_key, name)
    xml.delete_tag!(base_name)
  end

  response = request.execute!

  response.ok?
end

#remove_users!(users) ⇒ Object

Remove the given users from the current set of group users.



77
78
79
80
81
82
# File 'lib/reactor/cm/group.rb', line 77

def remove_users!(users)
  users = users.is_a?(Array) ? users : [users]
  users = self.users - users

  set_users(users)
end

#rename!(name) ⇒ Object

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.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/reactor/cm/group.rb', line 129

def rename!(name)
  new_attributes =
    self.class.attributes.each_with_object({}) do |mapping, hash|
      key, = mapping

      hash[key] = send(key)
    end

  if delete!
    new_attributes[:name] = name

    self.class.create(new_attributes)
  else
    false
  end
end

#save!Object

Saves all settable instance attributes to the Content Manager.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/reactor/cm/group.rb', line 97

def save!
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, self.class.primary_key, name)
    xml.set_tag!(base_name) do
      self.class.attributes(:set).each do |name, xml_attribute|
        value = send(name)

        xml.value_tag!(xml_attribute.name, value)
      end
    end
  end

  response = request.execute!

  response.ok?
end

#set_users!(users) ⇒ Object

Set the group users to the given users.



85
86
87
88
89
90
91
92
93
94
# File 'lib/reactor/cm/group.rb', line 85

def set_users!(users)
  request = XmlRequest.prepare do |xml|
    xml.where_key_tag!(base_name, self.class.primary_key, name)
    xml.set_key_tag!(base_name, self.class.xml_attribute(:users).name, users)
  end

  request.execute!

  self.users = users
end

#user?(name) ⇒ Boolean

Returns true, if an user with the given name exists, false otherwise.

Returns:

  • (Boolean)


64
65
66
# File 'lib/reactor/cm/group.rb', line 64

def user?(name)
  users.include?(name)
end