Merge remote-tracking branch 'origin/manage-ui' into next
[shapado:shapado.git] / lib / shapado / controllers / access.rb
1 module Shapado
2   module Controllers
3     module Access
4       def self.included(base)
5         base.class_eval do
6           helper_method :logged_in?
7         end
8       end
9
10       def logged_in?
11         user_signed_in?
12       end
13
14       def check_group_access
15         if ((!current_group.registered_only || is_bot?) && !current_group.private) || devise_controller? || (params[:controller] == "users" && action_name == "new" )
16           return
17         end
18
19         if logged_in?
20           if !current_user.user_of?(@current_group)
21             raise Goalie::Forbidden
22           end
23         else
24           respond_to do |format|
25             format.json { render :json => {:message => "Permission denied" }}
26             format.html { redirect_to new_user_session_path }
27           end
28         end
29       end
30
31       def admin_required
32         unless current_user.admin?
33           raise Goalie::Forbidden
34         end
35       end
36
37       def moderator_required
38         unless current_user.mod_of?(current_group)
39           raise Goalie::Forbidden
40         end
41       end
42
43       def owner_required
44         unless current_user.owner_of?(current_group)
45           raise Goalie::Forbidden
46         end
47       end
48
49       def login_required
50         respond_to do |format|
51           format.js do
52             if warden.authenticate(:scope => :user).nil?
53               return render(:json => {:message => t("global.please_login"),
54                                                 :success => false,
55                                                 :status => :unauthenticate}.to_json)
56             end
57           end
58           format.any { warden.authenticate!(:scope => :user) }
59         end
60       end
61
62       def after_sign_in_path_for(resource)
63         if current_user.admin?
64           Jobs::Activities.async.on_admin_connect(request.remote_ip, current_user.id).commit!
65         end
66         if current_user.facebook_login? && current_user.facebook_friends.empty?
67           Jobs::Users.async.get_facebook_friends(current_user.id).commit!
68         end
69         if current_user.twitter_login? && current_user.twitter_friends.empty?
70           Jobs::Users.async.get_twitter_friends(current_user.id).commit!
71         end
72         if current_user.identica_login? && current_user.identica_friends.empty?
73           Jobs::Users.async.get_identica_friends(current_user.id).commit!
74         end
75         if current_user.linked_in_login? && current_user.linked_in_friends.empty?
76           Jobs::Users.async.get_linked_in_friends(current_user.id).commit!
77         end
78         if return_to = session.delete("return_to")
79           return_to
80         else
81           super
82         end
83       end
84     end
85   end
86 end