Authorization for data on dashboard and index pages
[gitorious:mainline.git] / app / controllers / site_controller.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2012 Gitorious AS
4 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
5 #   Copyright (C) 2008 Johan Sørensen <johan@johansorensen.com>
6 #   Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
7 #   Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
8 #   Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
9 #
10 #   This program is free software: you can redistribute it and/or modify
11 #   it under the terms of the GNU Affero General Public License as published by
12 #   the Free Software Foundation, either version 3 of the License, or
13 #   (at your option) any later version.
14 #
15 #   This program is distributed in the hope that it will be useful,
16 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #   GNU Affero General Public License for more details.
19 #
20 #   You should have received a copy of the GNU Affero General Public License
21 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #++
23
24 class SiteController < ApplicationController
25   PAGES = [:about, :faq, :contact, :tos, :privacy_policy]
26   skip_before_filter :public_and_logged_in, :only => [:index].concat(PAGES)
27   before_filter :login_required, :only => [:dashboard]
28   renders_in_site_specific_context :except => PAGES
29   renders_in_global_context :only => PAGES
30
31   def index
32     if !current_site.subdomain.blank?
33       render_site_index
34     else
35       render_global_index
36     end
37   end
38
39   def public_timeline
40     render_public_timeline
41   end
42
43   def dashboard
44     redirect_to current_user
45   end
46
47   def about
48   end
49
50   def faq
51   end
52
53   def contact
54   end
55
56   protected
57   # Render a Site-specific index template
58   def render_site_index
59     all_projects = current_site.projects.find(:all, :order => "created_at asc")
60     @projects = filter_authorized(current_user, all_projects)
61     @teams = Group.all_participating_in_projects(@projects)
62     @top_repository_clones = Repository.most_active_clones_in_projects(@projects)
63     @latest_events = Event.latest_in_projects(25, @projects.map{|p| p.id })
64     render "site/#{current_site.subdomain}/index"
65   end
66
67   def render_public_timeline
68     @projects = filter(Project.find(:all, :limit => 10, :order => "id desc"))
69     @top_repository_clones = filter(Repository.most_active_clones)
70     @active_projects = filter(Project.most_active_recently(15))
71     @active_users = User.most_active
72     @active_groups = Group.most_active
73     @latest_events = Event.latest(25)
74     render :template => "site/index"
75   end
76
77   def render_dashboard
78     @user = current_user
79     @projects = filter(@user.projects.find(:all,
80                                            :include => [:tags, { :repositories => :project }]))
81     @repositories = current_user.commit_repositories
82     @events = @user.paginated_events_in_watchlist(:page => params[:page])
83     @messages = @user.messages_in_inbox(3)
84     @favorites = @user.watched_objects
85     @root = Breadcrumb::Dashboard.new(@user)
86     @atom_auto_discovery_url = watchlist_user_path(@user, :format => :atom)
87     render :template => "site/dashboard"
88   end
89
90   def render_gitorious_dot_org_in_public
91     @feed_items = Rails.cache.fetch("blog_feed:feed_items", :expires_in => 1.hour) do
92       BlogFeed.new("http://blog.gitorious.org/feed/").fetch
93     end
94     render :template => "site/public_index", :layout => "second_generation/application"
95   end
96
97   # Render the global index template
98   def render_global_index
99     if logged_in?
100       render_dashboard
101     elsif GitoriousConfig["is_gitorious_dot_org"]
102       render_gitorious_dot_org_in_public
103     else
104       render_public_timeline
105     end
106   end
107
108   def filter(collection)
109     filter_authorized(current_user, collection)
110   end
111 end