-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin_controller.rb
53 lines (38 loc) · 1.25 KB
/
admin_controller.rb
1
2
3
4
5
6
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
class AdminController < ApplicationController
before_filter :admin_required, :except => [:login, :signup]
before_filter :signup_first_user, :except => [:signup]
before_filter :no_more_than_one_signup, :only => [:signup]
cache_sweeper :post_sweeper, :only => [:new_post]
def signup
@admin = Admin.new
return unless request.post?
@admin.attributes = params[:admin]
session[:admin] = @admin if @admin.save!
redirect_to :action => :index and return if session[:admin]
end
def login
return unless request.post?
session[:admin] = Admin.find_by_username_and_password(params[:admin][:username],params[:admin][:password])
redirect_to :action => :index and return if session[:admin]
end
def index
end
def new_post
return unless request.post?
Post.create(params[:post])
redirect_to :action => :list_posts
end
def list_posts
@posts = Post.find :all, :order => "id DESC"
end
protected
def signup_first_user
redirect_to :action => :signup and return if Admin.count.zero?
end
def no_more_than_one_signup
redirect_to :action => :login and return if Admin.count > 0
end
def admin_required
redirect_to "/" and return unless session[:admin]
end
end