Consistency is nice
[gitorious:mainline.git] / app / models / team.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2012 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18 class Team
19
20   def self.group_implementation
21     @group_implementation
22   end
23
24   def self.group_implementation=(klass)
25     @group_implementation = klass
26   end
27
28   self.group_implementation = Group
29   
30   def self.paginate_all(current_page = nil)
31     group_implementation.paginate(:all, :page => current_page)
32   end
33
34   def self.find_by_name!(name)
35     includes = [:projects, :repositories, :committerships]
36     if group_implementation === Group
37       includes << :members
38     end
39     group_implementation.find_by_name!(name,
40                         :include => includes)
41   end
42
43   def self.memberships(group)
44     if group.is_a?(Group)
45       group.memberships.find(:all, :include => [:user, :role])
46     else
47       []
48     end
49   end
50
51   def self.events(group, page)
52     if group.is_a?(Group)
53       group.events(page)
54     else
55       []
56     end
57   end
58
59   def self.new_group
60     group_implementation.new
61   end
62
63   def self.create_group(params, user)
64     group = group_implementation.new(group_params(params))
65     group.transaction do
66       group.creator = user
67       group.save!
68       if group.is_a?(Group)
69         group.memberships.create!({
70                                     :user => user,
71                                     :role => Role.admin,
72                                   })
73       end
74     end
75     return group
76   end
77
78   def self.group_params(params)
79     params.key?(:ldap_group) ? params[:ldap_group] : params[:group]
80   end
81
82   
83   def self.update_group(group, params)
84     params = group_params(params)
85     group.description = params[:description]
86     group.avatar = params[:avatar]
87     group.save!
88   end
89
90   class DestroyGroupError < StandardError
91   end
92   
93   def self.destroy_group(name, user)
94     group = group_implementation.find_by_name! name
95     unless user.is_admin? 
96       raise DestroyGroupError, "You're not admin" unless group_admin?(group, user)
97       raise DestroyGroupError, "Teams with current members or projects cannot be deleted" unless group.deletable?
98     end
99     group.destroy
100   end
101
102   def self.group_admin?(group, user)
103     return false unless user.is_a? User
104     role = group.user_role(user)
105     role && role.admin?
106   end
107
108   def self.delete_avatar(group)
109     group.avatar.destroy
110     group.save
111   end
112
113   def self.can_have_members?(group)
114     group.is_a?(Group) 
115   end
116
117
118 end