initial import boxroom 0.6.2
[boxroom-stian.git] / app / models / group.rb
blob928c55ea0c87aa6dad68bbafa19c9235564e7e2d
1 # Groups are used to determine which groups of users have which rights
2 # on which folders.
3 class Group < ActiveRecord::Base
4   has_many :group_permissions
6   has_and_belongs_to_many :users
8   validates_uniqueness_of :name
9   validates_presence_of :name
11   before_destroy :dont_destroy_admins
12   # Don't delete 'admins' from the database
13   def dont_destroy_admins
14     raise "Can't delete admins group" if self.is_the_administrators_group?
15   end
17   after_destroy :destroy_dependant_group_permissions
18   # Delete dependant group_permissions.
19   # This code should be executed after_destroy.
20   def destroy_dependant_group_permissions
21     self.group_permissions.each do |group_permission|
22       group_permission.destroy
23     end
24   end
26   # Returns whether or not the admins group exists
27   def self.admins_group_exists?
28     group = Group.find_by_is_the_administrators_group(true)
29     return (not group.blank?)
30   end
32   # Create admins group and add admin user to it.
33   def self.create_admins_group
34     if User.admin_exists? # and Group.admins_group_exists?
35       group = Group.new
36       group.name = 'admins'
37       group.is_the_administrators_group = true
39       # Add the adminstrator to this group:
40       if user = User.find_by_is_the_administrator(true)
41         user.groups.push(group)
42       end
44       group.save # save, so true is returned
45     end
46   end
47 end