Support private repositories with LDAP authorization
[gitorious:thomas-mainline.git] / app / controllers / content_memberships_controller.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
19 class ContentMembershipsController < ApplicationController
20   before_filter :require_private_repos
21   before_filter :login_required
22   renders_in_site_specific_context :only => [:index]
23
24   def index
25     @memberships = paginate(page_free_redirect_options) do
26       content.content_memberships.paginate(:all, :page => params[:page])
27     end
28     @root = Breadcrumb.const_get("#{content.class.name}Memberships".to_sym).new(content)
29     render_action(:index)
30   end
31
32   def create
33     membership = content.content_memberships.new
34     membership.member = member(params[:user], params[:group])
35     membership.save
36     redirect_to :action => "index"
37   rescue ActiveRecord::RecordNotFound => err
38     m = err.message.match(/([^\s]+) with [^\s]+ = (.*)/)
39     flash[:error] = "No such #{m[1].downcase} '#{m[2]}'"
40     index
41   end
42
43   def destroy
44     if params[:id] == "all"
45       content.content_memberships.destroy_all
46     else
47       content.content_memberships.find(params[:id]).destroy
48     end
49     redirect_to :action => "index"
50   end
51
52   helper_method :memberships_path
53   helper_method :membership_path
54   helper_method :new_membership_path
55   helper_method :content_path
56
57   protected
58   def member(user, group)
59     return User.find_by_login!(user[:login]) if user && !user[:login].empty?
60     return Team.find_by_name!(group[:name]) if group && !group[:name].empty?
61     content.owner
62   end
63
64   def render_action(action)
65     @site_name = GitoriousConfig["site_name"]
66     @content = content
67     @class_name = @content.class.name.downcase
68     render :template => "content_memberships/#{action}"
69   end
70 end