TodoList: ユーザ認証の設定2006年12月06日 13時28分08秒

前回はプラグインをインストールしただけで、終わってしまった。今回は、user_engine の設定。


module LoginEngine
  config :salt, "your-salt-here"
  config :user_email_notification, false
end

module UserEngine
  config :admin_login, "uyota"
  config :admin_email, "uyota@example.com"
  config :admin_password, "XXXXX"
end

Engines.start :login
Engines.start :user

を config/environment.rb の末尾に追加。

app/controllers/application.rb を以下のように編集。


# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

require 'login_engine'

class ApplicationController < ActionController::Base
  include LoginEngine
  include UserEngine
  helper :user
  model :user
  #before_filter :login_required
  before_filter :authorize_action
  end
end

その後、app/helpers/application_helper.rb を以下のように編集。


module ApplicationHelper
  include LoginEngine
  include UserEngine
end

データベースを作るスクリプトと Makefile を作成。最初に make のターゲットを db にしたら、動かなかった。Rails が db というディレクトリを作っていたためだ。


% cat create_table.mysql 
DROP DATABASE todos;
CREATE DATABASE todos DEFAULT CHARACTER SET UTF8;
USE todos;
CREATE TABLE `todos`
(
  `id` INT NOT NULL AUTO_INCREMENT,
  `description` VARCHAR(100) NOT NULL,
  `done` TINYINT DEFAULT '0' NOT NULL,
  PRIMARY KEY(`id`)
);
% cat Makefile 
MYSQL = mysql -u root

database:
        cat create_table.mysql | ${MYSQL}
        rake db:migrate:engines:login_engine
        rake db:migrate:engines:user_engine
        rake bootstrap

make database で一気に必要なデータベースを作る。


% make
cat create_table.mysql | mysql -u root
rake db:migrate:engines:login_engine
(in /export/home/uyota/Todo)
Migrating engine 'login_engine'
== InitialSchema: migrating ===================================================
-- create_table("users", {:force=>true})
   -> 0.0303s
== InitialSchema: migrated (0.0330s) ==========================================

rake db:migrate:engines:user_engine
(in /export/home/uyota/Todo)
Migrating engine 'user_engine'
== InitialSchema: migrating ===================================================
-- create_table("permissions", {:force=>true})
   -> 0.0290s
-- create_table("permissions_roles", {:force=>true, :id=>false})
   -> 0.0279s
-- create_table("users_roles", {:force=>true, :id=>false})
   -> 0.0327s
-- create_table("roles", {:force=>true})
   -> 0.0279s
== InitialSchema: migrated (0.1284s) ==========================================

== AddedOmnipotentFlagToRoles: migrating ======================================
-- add_column(:roles, :omnipotent, :boolean, {:null=>false, :default=>false})
   -> 0.0584s
Couldn't find the admin role.
If this is not a migration or test, your authorization data may be corrupt.
== AddedOmnipotentFlagToRoles: migrated (0.0732s) =============================
-- add_column(:roles, :omnipotent, :boolean, {:null=>false, :default=>false})
   -> 0.0584s
Couldn't find the admin role.
If this is not a migration or test, your authorization data may be corrupt.
== AddedOmnipotentFlagToRoles: migrated (0.0732s) =============================

== AddedSystemRoles: migrating ================================================
-- add_column(:roles, :system_role, :boolean, {:null=>false, :default=>false})
   -> 0.0512s
== AddedSystemRoles: migrated (0.0585s) =======================================
rake bootstrap
(in /export/home/uyota/Todo)
Creating guest role 'Guest'
Creating admin role 'Admin'
Creating user role 'User'
Creating admin user 'uyota'

これで、http://localhost:3000/todo/list にアクセスすると http://localhost:3000/user/login に一度転送される。認証に成功すると、http://localhost:3000/todo/list に戻ってくる。

前回次回