Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

! fix auth controllers

  • Loading branch information...
commit 728800a1ff3df44fd1b92c6466bc583d6c1316ef 1 parent bc91f1e
@arcresu arcresu authored
View
46 app/controllers/accounts.rb
@@ -1,11 +1,22 @@
LeacockGallery.controllers :accounts do
- # Viewing
+ # Filters
+ # -------
+ #
+
+ before :show, :replace, :delete, :edit do
+ @account = Account.fetch(params[:slug])
+ if @account.nil?
+ halt 404, "We couldn't find an account called '#{params[:slug]}'."
+ end
+ end
+
+
+ # Display
# -------
#
get :show, map: "/accounts/:slug", provides: [:html] do
- @account = Account.first(slug: params[:slug])
render 'accounts/show'
end
@@ -15,29 +26,34 @@
#
post :create, map: "/accounts", provides: [:html] do
- @account = Account.new(params[:account])
+ attrs = params.symbolize_keys.only(:name, :slug, :email, :role,
+ :password, :password_confirmation)
+ # TODO make `role` and `password`s secure
+
+ @account = Account.new(attrs)
if @account.save
flash[:notice] = 'The account was successfully created.'
- redirect url(:accounts, :show, id: @account.id)
+ redirect url_for(:accounts, :show, slug: @account.slug)
else
- flash[:error] = 'There was a problem creating the account.'
- redirect url(:accounts, :new)
+ flash[:error] = "There was a problem creating the account. #{@account.errors.full_messages.to_sentence}"
+ redirect url_for(:accounts, :new)
end
end
put :replace, map: "/accounts/:slug", provides: [:html] do
- @account = Account.first(slug: params[:slug])
- if @account.update(params[:account])
+ attrs = params.symbolize_keys.only(:name, :slug, :email, :role,
+ :password, :password_confirmation)
+
+ if @account.attributes = attrs
flash[:notice] = 'The account was successfully updated.'
- redirect url(:accounts, :show, id: @account.id)
+ redirect url_for(:accounts, :show, slug: @account.slug)
else
flash[:error] = 'There was a problem updating the account.'
- redirect url(:accounts, :edit, id: @account.id)
+ redirect url_for(:accounts, :edit, slug: @account.slug)
end
end
delete :destroy, map: "/accounts/:slug", provides: [:html] do
- @account = Account.first(slug: params[:slug])
if @account != current_account && @account.destroy
flash[:notice] = 'The account was successfully destroyed.'
else
@@ -53,16 +69,16 @@
get :new, map: "/admin/accounts/new", provides: [:html] do
@account = Account.new
- render 'accounts/new'
+ render 'accounts/new', layout: :admin
end
get :edit, map: "/admin/accounts/:slug", provides: [:html] do
- @account = Account.first(slug: params[:slug])
- render 'accounts/edit'
+ render 'accounts/edit', layout: :admin
end
get :admin_index, map: "/admin/accounts", provides: [:html] do
@accounts = Account.all
- render 'accounts/admin_index'
+ render 'accounts/admin_index', layout: :admin
end
+
end
View
2  app/controllers/admin.rb
@@ -1,4 +1,6 @@
LeacockGallery.controllers :admin do
+ layout :admin
+
get :index do
render "admin/index"
end
View
11 app/controllers/sessions.rb
@@ -1,6 +1,7 @@
LeacockGallery.controllers :sessions, map: "/admin" do
+ layout false
- get :new, map: "/admin/login", provides: [:html] do
+ get :create, map: "/admin/login", provides: [:html] do
render "/sessions/new", nil, layout: false
end
@@ -8,21 +9,21 @@
account = Account.authenticate(params[:email], params[:password])
if account
set_current_account(account)
- redirect url(:base, :index)
+ redirect url_for(:admin, :index)
elsif Padrino.env == :development && params[:bypass]
account = Account.first
set_current_account(account)
- redirect url(:base, :index)
+ redirect url_for(:admin, :index)
else
params[:email], params[:password] = h(params[:email]),
h(params[:password])
flash[:warning] = "Login or password wrong."
- redirect url(:sessions, :new)
+ redirect url_for(:sessions, :create)
end
end
delete :destroy, map: "/admin/login", provides: [:html] do
set_current_account(nil)
- redirect url(:sessions, :new)
+ redirect url_for(:sessions, :create)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.