Guest
Public paste!

Admin controller

By: a guest | Mar 7th, 2010 | Syntax: Rails | Size: 2.42 KB | Hits: 182 | Expires: Never
Copy text to clipboard
  1. require "digest/sha2"
  2. require "RMagick"
  3.  
  4. class AdminController < ApplicationController
  5.         layout "sweet"
  6.         def index
  7.                 unless session[:current_user].nil?
  8.                         redirect_to({:action => "interface"})
  9.                 end
  10.                 @valeur_captcha = rand(99999).to_s
  11.                 captcha = Magick::Image.new(100, 15)
  12.                 texte = Magick::Draw.new
  13.                 texte.gravity = Magick::CenterGravity
  14.                 texte.annotate(captcha, 0, 0, 0, 0, @valeur_captcha)
  15.                 captcha.write("#{RAILS_ROOT}/public/images/captcha/#{request.env['REMOTE_ADDR']}.jpg")
  16.         end
  17.         def connexion
  18.                 @admin_tst = Admin.new(params[:admin])
  19.                 if @admin_tst[:captcha] == @valeur_captcha
  20.                         @admin = Admin.find(:first, {:conditions => ["pseudo= ? and pass= ?", @admin_tst.pseudo, Digest::SHA512.hexdigest(@admin_tst.pass)]})
  21.                         if @admin.nil? then
  22.                                 @error_message = true
  23.                                 @admin = @admin_tst
  24.                                 render({ :action => "index"})
  25.                                 badguy_ip = File.new("#{RAILS_ROOT}/public/badguy", "a+")
  26.                                 @list_badguy_ip = badguy_ip.readlines
  27.                                 suspect = File.new("#{RAILS_ROOT}/public/suspect", "a+")
  28.                                 list_suspect_ip = suspect.readlines
  29.                                 @ip = request.env['REMOTE_ADDR']
  30.                                 if list_suspect_ip.include?(@ip) then
  31.                                         unless (@list_badguy_ip.include?(@ip))
  32.                                                 badguy_ip.write @ip
  33.                                         end
  34.                                         badguy_ip.close
  35.                                 end
  36.                                 cookies[:try] = {:value => "01", :expires => 24.hour.from_now}
  37.                                 unless (list_suspect_ip.include?(@ip))
  38.                                         suspect.write @ip
  39.                                 end
  40.                                 suspect.close
  41.                         else
  42.                                 session[:current_user] = @admin
  43.                                 redirect_to({ :action => "interface"})
  44.                         end
  45.                 else
  46.                 @bad_captcha = true
  47.                 render({ :action => "index"})
  48.                 end
  49.         end
  50.         def interface
  51.                 if session[:current_user].nil? then
  52.                         redirect_to({ :action => "index"})
  53.                 else
  54.                         @news = News.find(:all, :order => "created_at DESC")
  55.                 end
  56.         end
  57.         def ajout
  58.                 if session[:current_user].nil? then
  59.                         redirect_to({ :action => "index"})
  60.                 else
  61.                         if request.post? then
  62.                                 @news = News.new(params[:idnews])
  63.                                 @news.save
  64.                                 redirect_to(:action => "interface")
  65.                         end
  66.                 end
  67.         end
  68.         def suppression
  69.                 if session[:current_user].nil? then
  70.                         redirect_to({ :action => "index"})
  71.                 else
  72.                         News.find(params[:id]).destroy
  73.                         redirect_to(:action => "interface")
  74.                 end
  75.         end
  76.         def modification
  77.                 if session[:current_user].nil? then
  78.                         redirect_to({ :action => "index"})
  79.                 else
  80.                         @news = News.find(params[:id])
  81.                         if request.post? then
  82.                                 @news.update_attributes(params[:news])
  83.                                 redirect_to(:action => "interface")
  84.                         end
  85.                 end
  86.         end
  87. end