Class: Group

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
RubyLess
Defined in:
app/models/group.rb

Overview

Groups are used for access control. They cannot be used cross-site like users.

Three groups cannot be destroyed and have a special meaning in each site (set in Site) :

public

Access for this group is granted to all visitors regardless of user login.

site

All users except anonymous user are in this group. It is the ‘logged in’ users’ group.

admin

A user in this group is automatically added to all groups. He/she can add or remove

users, change user groups, monitor content, etc.

Only administrators can change groups. An administrator cannot remove him/herself from the admin group.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.translate_pseudo_id(id, sym = :id) ⇒ Object

FIXME: test translate_pseudo_id for groups



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/group.rb', line 26

def self.translate_pseudo_id(id,sym=:id)
  str = id.to_s
  if str =~ /\A\d+\Z/
    # id
    Zena::Db.fetch_attribute("SELECT #{sym} FROM groups WHERE site_id = #{current_site[:id]} AND id = '#{str}'")
  elsif str =~ /\A([a-zA-Z ]+)(\+*)\Z/
    Zena::Db.fetch_attribute("SELECT groups.#{sym} FROM groups WHERE site_id = #{current_site[:id]} AND name LIKE #{self.connection.quote("#{$1}%")} LIMIT 1 OFFSET #{$2.size}")
  else
    nil
  end
end

Instance Method Details

#active_usersObject



71
72
73
74
# File 'app/models/group.rb', line 71

def active_users
  User.find(:all, :conditions => ['groups_users.group_id = ? AND status > ?', self.id, User::Status[:deleted]],
                  :joins => 'INNER JOIN groups_users ON users.id = groups_users.user_id')
end

#can_destroy?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
# File 'app/models/group.rb', line 85

def can_destroy?
  clause = [:rgroup_id, :wgroup_id, :dgroup_id].map{|g| "#{g} = '#{self[:id]}'"}.join(" OR ")
  if 0 == self.class.count_by_sql("SELECT COUNT(*) FROM #{Node.table_name} WHERE #{clause}")
    return true
  else
    errors.add('base', 'this group is used by node access definitions')
    return false
  end
end

#o_usersObject



60
# File 'app/models/group.rb', line 60

alias o_users users

#public_group?Boolean

Return true if the group is the public group of the site.

Returns:

  • (Boolean)


39
40
41
# File 'app/models/group.rb', line 39

def public_group?
  self[:id] == visitor.site[:public_group_id]
end

#replace_byObject



81
82
83
# File 'app/models/group.rb', line 81

def replace_by
  @replace_by
end

#replace_by=(group_id) ⇒ Object

Replace all uses of the group (rgroup_id, wgroup_id, dgroup_id) by another group.



77
78
79
# File 'app/models/group.rb', line 77

def replace_by=(group_id)
  @replace_by = group_id unless group_id.blank?
end

#site_group?Boolean

Return true if the group is the site group.

Returns:

  • (Boolean)


44
45
46
# File 'app/models/group.rb', line 44

def site_group?
  self[:id] == visitor.site[:site_group_id]
end

#user_idsObject



48
49
50
# File 'app/models/group.rb', line 48

def user_ids
  @user_ids ||= users.map {|r| r[:id]}
end

#user_ids=(list) ⇒ Object



52
53
54
55
56
57
58
# File 'app/models/group.rb', line 52

def user_ids=(list)
  if public_group? || site_group?
    # ignore
  else
    @defined_user_ids = list
  end
end

#usersObject



61
62
63
64
65
66
67
68
69
# File 'app/models/group.rb', line 61

def users
  @users ||= begin
    usr = o_users
    usr.each do |r|
      r[:password] = nil
    end
    usr
  end
end