Module: SharkApps::InstanceMethods

Defined in:
lib/sharkapps.rb

Instance Method Summary collapse

Instance Method Details

#check_account_authorizedObject

redirect to the not_subscribed path if not authorized



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sharkapps.rb', line 74

def 
  return true if SharkApps.debug_mode
  res = @account.approval_status
  need_check = @account.last_approval_check.nil? ? true : ((Time.now.to_i - @account.last_approval_check.to_i) > 5.minutes.to_i)
  if need_check
    #get the account
    AppformaAccount.(@account)
    res = @account.approval_status
  end
  if res == Subscription::EXPIRED || res == Subscription::UNAUTHORIZED
    redirect_to not_subscribed_path(:id => @account.acc_hash)
  end 
end

#check_adminObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sharkapps.rb', line 108

def check_admin
  #check that we have a Facebook user. if not, redirect.
  @oauth = Koala::Facebook::OAuth.new(Facebooker2.app_id, Facebooker2.secret)
  
  #returning from the oauth server
 if params[:code] and session[:access_token].nil?
   @access_token = @oauth.get_access_token(params[:code], :redirect_uri => session[:redirect_uri])
   session[:redirect_uri] = nil
   session[:access_token] = @access_token
 end
 
 #no access token stored - do a redirect
 unless session[:access_token]
   session[:redirect_uri] = request.url
   redirect_to @oauth.url_for_oauth_code(:permissions => "publish_stream, manage_pages, email", :redirect_uri => request.url) and return
 end
 
 #validate that the access token that we have is good. if not, request a new one.
 begin
   fb = current_fb_user
 rescue Exception
   session[:redirect_uri] = request.url
   session[:access_token] = nil
   redirect_to @oauth.url_for_oauth_code(:permissions => "publish_stream, manage_pages, email", :redirect_uri => request.url) and return
 end
 
 #we have an access token, we can do the is_admin check now
  unless session[:access_token].nil?
      redirect_to not_authorized_url(:id => @account.acc_hash) unless is_admin
  end
end

#current_fb_userObject



176
177
178
179
180
# File 'lib/sharkapps.rb', line 176

def current_fb_user
  return @current_fb_user if @current_fb_user
  @current_fb_user = Koala::Facebook::API.new(session[:access_token]).get_object("me") if session[:access_token]
  @current_fb_user
end

#facebook_session_from_cookiesObject



162
163
164
165
166
167
# File 'lib/sharkapps.rb', line 162

def facebook_session_from_cookies
  puts "request cookies = #{request.cookies}"
  oauth = Koala::Facebook::OAuth.new(Facebooker2.app_id, Facebooker2.secret)
  facebook_session = oauth.(request.cookies)
  facebook_session
end

#facebook_user_from_cookiesObject



169
170
171
172
173
174
# File 'lib/sharkapps.rb', line 169

def facebook_user_from_cookies
  facebook_session = facebook_session_from_cookies
  @facebook_user = Koala::Facebook::API.new(facebook_session["access_token"]).get_object("me") if facebook_session
  @access_token = facebook_session["access_token"] if facebook_session
  @facebook_user
end

#is_adminObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sharkapps.rb', line 140

def is_admin
  return true if SharkApps.debug_mode
  
  if fb_load_facebook_params['page']
    fb_load_facebook_params['page']['admin']
  else
    
    #go by the facebook user ID and make an API call. might need to store it in the session
    sym = ("auth_admin_" + @account.fbid).to_sym
    puts "symbol: #{sym}"
    puts "symbol value: #{session[sym]}"
    puts "symbol checking for false: #{session[sym] == false}"
    need_check = (session[sym].nil? or session[sym] == false)
    puts "need check: = #{need_check}"
    if need_check #need_check
      return false if current_fb_user.nil?
      session[sym] = AppformaAccount.check_admin_authorized(@account, current_fb_user['id'])
    end
    return session[sym]
  end    
end

#set_tab_current_accountObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sharkapps.rb', line 88

def 
  if params[:wib_fbid]
    @account = Account.find_or_create_by_fbid(params[:wib_fbid])
    @isliked = true
    @external = true
    @wibiya_mode = true
  else
    #hack for fbml 
    temp = fb_load_facebook_params 
    page_id = temp["page"]["id"]
    @account = Account.find_or_create_by_fbid(page_id)
    #set the default locale if supported, and if the account doesn't have a locale set
    if @account.respond_to? :locale and @account.locale.blank?
      @account.locale = "il"
      @account.save
    end
    @isliked = temp["page"]["liked"]
  end
end