Module: Sinatra::RedSlash

Defined in:
lib/redsafe.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/redsafe.rb', line 7

def self.registered(app)
  #INVESTIGATE
  #the possibility of sinatra having an array of view_paths to load from
  #PROBLEM
  #sinatra 9.1.1 doesn't have multiple view capability anywhere
  #so to get around I have to do it totally manually by
  #loading the view from this path into a string and rendering it
  set :red_slash_view_path, Pathname(__FILE__).dirname.expand_path + "views/"

  get '/users' do
    
    redirect "/" unless current_user.admin?

    @users = User.all
    if @users != []
      haml get_view_as_string("index.haml"), :layout => use_layout?
    else
      redirect '/signup'
    end
  end

  get '/users/:id' do
    

    @user = User.get(:id => params[:id])
    haml get_view_as_string("show.haml"), :layout => use_layout?
  end

  #convenience for ajax but maybe entirely stupid and unnecesary
  get '/logged_in' do
    if session[:user]
      "true"
    else
      "false"
    end
  end

  get '/login' do
    haml get_view_as_string("login.haml"), :layout => use_layout?
  end

  post '/login' do
    if user = User.authenticate(params[:email], params[:password])
      session[:user] = user.id
      if session[:return_to]
        redirect_url = session[:return_to]
        session[:return_to] = false
        redirect redirect_url
      else
        redirect '/'
      end
    else
      redirect '/login'
    end
  end

  get '/logout' do
    session[:user] = nil
    @message = "in case it weren't obvious, you've logged out"
    redirect '/'
  end

  get '/signup' do
    haml get_view_as_string("signup.haml"), :layout => use_layout?
  end

  post '/signup' do
    @user = User.set(params[:user])
    if @user
      session[:user] = @user.id
      redirect '/'
    else
      session[:flash] = "failure!"
      redirect '/'
    end
  end

  get '/users/:id/edit' do
    
    redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]

    @user = User.get(:id => params[:id])
    haml get_view_as_string("edit.haml"), :layout => use_layout?
  end

  post '/users/:id/edit' do
    
    redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]

    user = User.get(:id => params[:id])
    user_attributes = params[:user]
    if params[:user][:password] == ""
        user_attributes.delete("password")
        user_attributes.delete("password_confirmation")
    end

    if user.update(user_attributes)
      redirect '/'
    else
      session[:notice] = 'whoops, looks like there were some problems with your updates'
      redirect "/users/#{user.id}/edit"
    end
  end

  get '/users/:id/delete' do
    
    redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]

    if User.delete(params[:id])
      session[:flash] = "way to go, you deleted a user"
    else
      session[:flash] = "deletion failed, for whatever reason"
    end
    redirect '/'
  end


  if Sinatra.const_defined?('FacebookObject')
    get '/connect' do
      if fb[:user]
        if current_user.class != GuestUser
          user = current_user
        else
          user = User.get(:fb_uid => fb[:user])
        end

        if user
          if !user.fb_uid || user.fb_uid != fb[:user]
            user.update :fb_uid => fb[:user]
          end
          session[:user] = user.id
        else
          user = User.set!(:fb_uid => fb[:user])
          session[:user] = user.id
        end
      end
      redirect '/'
    end

    get '/receiver' do
      %[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" >
          <body>
            <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.js" type="text/javascript"></script>
          </body>
        </html>]
    end
  end
end