Commit 2704d967 authored by 神楽坂玲奈's avatar 神楽坂玲奈

用户组模型初步

git-svn-id: http://glupx.googlecode.com/svn/trunk/Reliz@81 189f022a-1064-8ae2-3e6f-c4a67275c50b
parent 3ba913e7
...@@ -12,14 +12,14 @@ class ApplicationController < ActionController::Base ...@@ -12,14 +12,14 @@ class ApplicationController < ActionController::Base
end end
def load_user def load_user
if session[:user_id] if session[:user_id]
@correct_user = User.find session[:user_id] @corrent_user = User.find session[:user_id]
end end
unless @correct_user unless @corrent_user
@correct_user = User::Guest @corrent_user = User::Guest
end end
end end
def load_locale def load_locale
locale = @correct_user.locale || ( locale = @corrent_user.locale || (
request_language = request_language =
request_language && request_language['HTTP_ACCEPT_LANGUAGE'][/[^,;]+/] request_language && request_language['HTTP_ACCEPT_LANGUAGE'][/[^,;]+/]
) )
...@@ -31,8 +31,8 @@ class ApplicationController < ActionController::Base ...@@ -31,8 +31,8 @@ class ApplicationController < ActionController::Base
case #TODO: DRY case #TODO: DRY
when @site[:themes].has_key?(cookies[:theme]) when @site[:themes].has_key?(cookies[:theme])
prepend_view_path File.join Themes_Dir, cookies[:theme] prepend_view_path File.join Themes_Dir, cookies[:theme]
when @site[:themes].has_key?(@correct_user.theme) when @site[:themes].has_key?(@corrent_user.theme)
prepend_view_path File.join Themes_Dir, @correct_user.theme prepend_view_path File.join Themes_Dir, @corrent_user.theme
when @site[:themes].has_key?(@site.theme) when @site[:themes].has_key?(@site.theme)
prepend_view_path File.join Themes_Dir, @site.theme prepend_view_path File.join Themes_Dir, @site.theme
end end
......
...@@ -41,7 +41,7 @@ class ModerationsController < ApplicationController ...@@ -41,7 +41,7 @@ class ModerationsController < ApplicationController
# POST /moderations.xml # POST /moderations.xml
def create def create
@moderation = Moderation.new(params[:moderation]) @moderation = Moderation.new(params[:moderation])
@moderation.user = @correct_user @moderation.user = @corrent_user
case params[:moderation][:assocation_type] case params[:moderation][:assocation_type]
when "topic" when "topic"
#@moderation.association_ = Topic.find params[:moderation][:assocation_id] #@moderation.association_ = Topic.find params[:moderation][:assocation_id]
......
...@@ -53,7 +53,7 @@ class PostsController < ApplicationController ...@@ -53,7 +53,7 @@ class PostsController < ApplicationController
if @post.topic.nil? or @post.topic.locked if @post.topic.nil? or @post.topic.locked
return render :text => "Topic not found or locked." return render :text => "Topic not found or locked."
end end
@post.user = @correct_user @post.user = @corrent_user
@post.displayorder = @post.topic.floor @post.displayorder = @post.topic.floor
respond_to do |format| respond_to do |format|
if @post.save if @post.save
......
...@@ -50,6 +50,7 @@ class TopicsController < ApplicationController ...@@ -50,6 +50,7 @@ class TopicsController < ApplicationController
@posts = Post.find_all_by_topic_id(params[:id], :offset => 10*@page-10, :limit => 10, :order => :displayorder) @posts = Post.find_all_by_topic_id(params[:id], :offset => 10*@page-10, :limit => 10, :order => :displayorder)
@topic.update_attribute(:viewnum, @topic.viewnum + 1) @topic.update_attribute(:viewnum, @topic.viewnum + 1)
@corrent_user.update_attribute(:viewnum, @corrent_user.viewnum + 1)
respond_to do |format| respond_to do |format|
format.html # show.html.erb format.html # show.html.erb
format.xml { render :xml => params[:page] && !params[:page].empty? ? @posts : @topic } format.xml { render :xml => params[:page] && !params[:page].empty? ? @posts : @topic }
...@@ -83,13 +84,13 @@ class TopicsController < ApplicationController ...@@ -83,13 +84,13 @@ class TopicsController < ApplicationController
#TODO: 验证category_id有效性 #TODO: 验证category_id有效性
@topic = Topic.new(params[:topic]) @topic = Topic.new(params[:topic])
#@topic.category_id = Board.find(params[:topic][:category_id]) #@topic.category_id = Board.find(params[:topic][:category_id])
@topic.user = @correct_user @topic.user = @corrent_user
@topic.displayorder = 0 @topic.displayorder = 0
@post = Post.new(params[:post]) @post = Post.new(params[:post])
@post.displayorder = 1 @post.displayorder = 1
@post.topic = @topic @post.topic = @topic
@post.user = @correct_user @post.user = @corrent_user
respond_to do |format| respond_to do |format|
if @topic.save && @post.save if @topic.save && @post.save
......
...@@ -97,12 +97,13 @@ class UsersController < ApplicationController ...@@ -97,12 +97,13 @@ class UsersController < ApplicationController
respond_to do |format| respond_to do |format|
if @user if @user
session[:user_id] = @user.id session[:user_id] = @user.id
@user.update_attribute(:lastloginip, request.remote_ip)
format.html { redirect_to(:root, :notice => 'Login Successfully.') } format.html { redirect_to(:root, :notice => 'Login Successfully.') }
format.xml { head :ok } format.xml { head :ok }
else else
@user = User.new(params[:user]) @user = User.new(params[:user])
#@user.errors.add 'incorrect_username_or_password' #TODO: 查API #@user.errors.add 'incorrent_username_or_password' #TODO: 查API
return render :text => 'incorrect_username_or_password' return render :text => 'incorrent_username_or_password'
format.html { render :action => "login" } format.html { render :action => "login" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity } format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end end
...@@ -120,10 +121,10 @@ class UsersController < ApplicationController ...@@ -120,10 +121,10 @@ class UsersController < ApplicationController
#p params[:theme], @site[:themes].has_key?(params[:theme]) #p params[:theme], @site[:themes].has_key?(params[:theme])
if params[:theme].blank? if params[:theme].blank?
cookies[:theme] = nil cookies[:theme] = nil
@correct_user.update_attribute(:theme, nil) @corrent_user.update_attribute(:theme, nil)
elsif @site[:themes].has_key? params[:theme] elsif @site[:themes].has_key? params[:theme]
cookies[:theme] = params[:theme] cookies[:theme] = params[:theme]
@correct_user.update_attribute(:theme, params[:theme]) @corrent_user.update_attribute(:theme, params[:theme])
end end
respond_to do |format| respond_to do |format|
format.html { redirect_to :back } format.html { redirect_to :back }
......
module ApplicationHelper module ApplicationHelper
def logged? def logged?
@correct_user != User::Guest @corrent_user != User::Guest
end end
end end
\ No newline at end of file
class Role < ActiveRecord::Base
end
class User < ActiveRecord::Base class User < ActiveRecord::Base
#belongs_to :usergroup belongs_to :role
#belongs_to :admingroup
validates :name, :presence => true, validates :name, :presence => true,
:length => {:minimum => 1, :maximum => 254} :length => {:minimum => 1, :maximum => 254}
validates :email, :presence => true, validates :email, :presence => true,
...@@ -9,11 +9,11 @@ class User < ActiveRecord::Base ...@@ -9,11 +9,11 @@ class User < ActiveRecord::Base
:format => {:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i} :format => {:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i}
has_many :topics has_many :topics
has_many :posts, :through => :topics has_many :posts, :through => :topics
before_save proc{!locked}
class <<self #TODO class <<self #TODO
alias old_find find alias old_find find
def find(*args) def find(*args)
if args[0] == 0 if args[0].to_i.zero?
Guest Guest
else else
old_find(*args) old_find(*args)
...@@ -34,42 +34,20 @@ class User < ActiveRecord::Base ...@@ -34,42 +34,20 @@ class User < ActiveRecord::Base
end end
"<a href=\"/users/#{id}\"><img src=\"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.strip.downcase)}?size=#{size}\" /></a>".html_safe "<a href=\"/users/#{id}\"><img src=\"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.strip.downcase)}?size=#{size}\" /></a>".html_safe
end end
=begin def credit
t.string :name, :null => false credit1
t.string :nickname, :default => '', :null => false end
t.string :password, :null => false
t.string :email, :default => '', :null => false
t.references :usergroup, :default => '', :null => false
t.references :admingroup, :default => '', :null => false
t.string :regip, :default => '', :null => false
t.string :lastloginip, :default => '', :null => false
t.integer :readnum, :default => 0, :null => false
t.integer :viewnum, :default => 0, :null => false
t.integer :onlinetime, :default => 0, :null => false
t.integer :credit, :default => 0, :null => false
t.integer :credit1, :default => 0, :null => false
t.integer :credit2, :default => 0, :null => false
t.integer :credit3, :default => 0, :null => false
t.integer :credit4, :default => 0, :null => false
t.integer :credit5, :default => 0, :null => false
t.integer :credit6, :default => 0, :null => false
t.integer :credit7, :default => 0, :null => false
t.integer :credit8, :default => 0, :null => false
=end
Guest = User.new do |user| Guest = User.new do |user|
user.id = 0 user.id = 0
user.name = 'guest' user.name = 'guest'
user.nickname = '' user.nickname = ''
user.password = '' user.password = ''
user.email = '' user.email = ''
#usergroup = 0 user.role_id = 6
#admingroup = 0
user.regip = '127.0.0.1' user.regip = '127.0.0.1'
user.lastloginip = '127.0.0.1' user.lastloginip = '127.0.0.1'
user.readnum = 0
user.viewnum = 0 user.viewnum = 0
user.onlinetime = 0 user.onlinetime = 0
user.credit = 0
user.credit1 = 0 user.credit1 = 0
user.credit2 = 0 user.credit2 = 0
user.credit3 = 0 user.credit3 = 0
......
...@@ -25,9 +25,9 @@ ...@@ -25,9 +25,9 @@
<% if logged? %> <% if logged? %>
<tr> <tr>
<td rowspan="2" width="80"> <td rowspan="2" width="80">
<div id="dtx"><%= @correct_user.avatar :small %></div> <div id="dtx"><%= @corrent_user.avatar :small %></div>
</td> </td>
<td width="700" height="28"style="padding-left:5px">欢迎回来 <%= @correct_user %></td> <td width="700" height="28"style="padding-left:5px">欢迎回来 <%= @corrent_user %></td>
<td width="400" align="right" style="padding-right:10px" >欢迎新会员 <%= User.last %></td> <td width="400" align="right" style="padding-right:10px" >欢迎新会员 <%= User.last %></td>
</tr> </tr>
<% end %> <% end %>
......
<div id="user"> <div id="user">
<% if logged? %> <% if logged? %>
<cite><a href="space.php?uid=359" class="noborder"><%= @correct_user %> </a></cite> <cite><a href="space.php?uid=359" class="noborder"><%= @corrent_user %> </a></cite>
<span class="pipe">|</span> <span class="pipe">|</span>
<%= link_to t('user.my'), @correct_user %> <%= link_to t('user.my'), @corrent_user %>
<%= link_to t('user.space'), @correct_user %> <%= link_to t('user.space'), @corrent_user %>
<%= link_to t('user.notice'), @correct_user %> <%= link_to t('user.notice'), @corrent_user %>
<span id="myprompt_check"></span> <span id="myprompt_check"></span>
<%= link_to t('user.pm'), @correct_user %> <%= link_to t('user.pm'), @corrent_user %>
<span class="pipe">|</span> <span class="pipe">|</span>
<%= link_to t('user.logout'), logout_path %> <%= link_to t('user.logout'), logout_path %>
......
...@@ -185,7 +185,7 @@ ...@@ -185,7 +185,7 @@
<table cellspacing="0" cellpadding="0"> <table cellspacing="0" cellpadding="0">
<tbody><tr> <tbody><tr>
<td class="postauthor"> <td class="postauthor">
<div class="avatar"><%= @correct_user.avatar %></td> <div class="avatar"><%= @corrent_user.avatar %></td>
<td class="postcontent"> <td class="postcontent">
<!--<input type="hidden" name="formhash" value="ba0236f6"> <!--<input type="hidden" name="formhash" value="ba0236f6">
<input type="hidden" name="subject" value=""> <input type="hidden" name="subject" value="">
......
Reliz::Application.routes.draw do Reliz::Application.routes.draw do
resources :links
resources :settings
resources :moderations
resources :boards
root :to => 'boards#index'
resources :users root :to => 'boards#index'
get 'theme/:theme' => 'users#theme', :as => :theme
get 'login' => 'users#login'
post 'login' => 'users#login_do'
get 'logout' => 'users#logout'
get 'register' => 'users#new'
resources :blocks
resources :boards resources :boards
#match 'forum' => 'boards#index'
#match 'forum/:id/edit' => 'boards#edit', :id => /\d+/
#match 'forum/:id' => 'boards#show', :id => /\d+/
resources :posts
match 'topics/:topic_id/new' => 'posts#new', :topic_id => /\d+/ #
resources :topics resources :topics
#match 'topic/:id/(/:page)' => 'topics#show', :id => /\d+/, :page => /\d+/ resources :posts
match 'boards/:board_id/new' => 'topics#new', :board_id => /\d+/ # resources :users
#get 'topics/control' => 'topics#control' get 'theme/:theme' => 'users#theme', :as => :theme
get 'login' => 'users#login'
#match '(:something)(/:id)/:anything/', :controller => 'application', :action => 'redirect_to_thc', :something => /boards|topics|posts|users/, :id => /\d+/, :anything => /.*/ post 'login' => 'users#login_do'
get 'logout' => 'users#logout'
get 'register' => 'users#new'
resources :links
resources :moderations
# The priority is based upon order of creation: # The priority is based upon order of creation:
# first created -> highest priority. # first created -> highest priority.
......
...@@ -15,7 +15,7 @@ class CreateTopics < ActiveRecord::Migration ...@@ -15,7 +15,7 @@ class CreateTopics < ActiveRecord::Migration
t.boolean :reverse, :default => false, :null => false t.boolean :reverse, :default => false, :null => false
t.boolean :private, :default => false, :null => false t.boolean :private, :default => false, :null => false
t.timestamps #t.timestamps
end end
end end
......
...@@ -5,12 +5,10 @@ class CreateUsers < ActiveRecord::Migration ...@@ -5,12 +5,10 @@ class CreateUsers < ActiveRecord::Migration
t.string :nickname, :default => '', :null => false t.string :nickname, :default => '', :null => false
t.string :password, :null => false t.string :password, :null => false
t.string :email, :default => '', :null => false t.string :email, :default => '', :null => false
t.references :usergroup, :default => '', :null => false t.integer :role_id, :default => 5, :null => false
t.references :admingroup, :default => '', :null => false
t.boolean :locked, :default => false, :null => false t.boolean :locked, :default => false, :null => false
t.string :regip, :default => '', :null => false t.string :regip, :default => '', :null => false
t.string :lastloginip, :default => '', :null => false t.string :lastloginip, :default => '', :null => false
t.integer :readnum, :default => 0, :null => false
t.integer :viewnum, :default => 0, :null => false t.integer :viewnum, :default => 0, :null => false
t.integer :onlinetime, :default => 0, :null => false t.integer :onlinetime, :default => 0, :null => false
t.integer :credit, :default => 0, :null => false t.integer :credit, :default => 0, :null => false
......
...@@ -15,7 +15,7 @@ class CreateBoards < ActiveRecord::Migration ...@@ -15,7 +15,7 @@ class CreateBoards < ActiveRecord::Migration
t.integer :postperm, :default => 0, :null => false t.integer :postperm, :default => 0, :null => false
t.timestamps #t.timestamps
end end
end end
......
class CreateRoles < ActiveRecord::Migration
def self.up
create_table :roles do |t|
t.string :name, :null => false
t.boolean :topic_delete, :null => false, :default => false
t.integer :topic_digest, :null => false, :default => 0
t.integer :topic_displayorder, :null => false, :default => 0
t.boolean :topic_highlight, :null => false, :default => false
t.boolean :topic_edit, :null => false, :default => false
t.boolean :topic_lock, :null => false, :default => false
end
end
def self.down
drop_table :roles
end
end
class DemoData < ActiveRecord::Migration class DemoData < ActiveRecord::Migration
def self.up def self.up
=begin
Board.create :id => 1, Board.create :id => 1,
:superboard_id => 0, :superboard_id => 0,
:name => "综合讨论中心" :name => "综合讨论中心"
...@@ -20,28 +21,92 @@ class DemoData < ActiveRecord::Migration ...@@ -20,28 +21,92 @@ class DemoData < ActiveRecord::Migration
:url => "http://lab.66rpg.com:3000/", :url => "http://lab.66rpg.com:3000/",
:logo => "http://bbs.66rpg.com/logo.gif" :logo => "http://bbs.66rpg.com/logo.gif"
Navigation.create :id => 1, Navigation.create :id => 1,
:name => "论坛", :name => "论坛",
:displayorder => 0, :displayorder => 0,
:url => "/boards", :url => "/boards",
:usergroup_id => 0, :Role_id => 0,
:super_id => 0 :super_id => 0
Navigation.create :id => 2, Navigation.create :id => 2,
:name => "测试导航1", :name => "测试导航1",
:displayorder => 1, :displayorder => 1,
:url => "/boards", :url => "/boards",
:usergroup_id => 0, :Role_id => 0,
:super_id => 1 :super_id => 1
Navigation.create :id => 3, Navigation.create :id => 3,
:name => "测试导航2", :name => "测试导航2",
:displayorder => 1, :displayorder => 1,
:url => "/boards", :url => "/boards",
:usergroup_id => 0, :Role_id => 0,
:super_id => 1 :super_id => 1
User.create :id => 1,
:name => "admin",
:nickname => "华丽丽滴全能高管",
:password => "admin",
:email => "zh99998@163.com",
:role_id => 1,
:locked => false,
:regip => '127.0.0.1',
:lastloginip => '127.0.0.1',
:viewnum => 0,
:onlinetime => 0,
:credit => 0,
:credit1 => 0,
:credit2 => 0,
:credit3 => 0,
:credit4 => 0,
:credit5 => 0,
:credit6 => 0,
:credit7 => 0,
:credit8 => 0,
:locale => nil,
:theme => nil
Role.create :id => 1,
:name => "创始人",
:topic_delete => true,
:topic_digest => 3,
:topic_displayorder => 3,
:topic_highlight => true,
:topic_edit => true,
:topic_lock => true
Role.create :id => 2,
:name => "管理员",
:topic_delete => true,
:topic_digest => 3,
:topic_displayorder => 3,
:topic_highlight => true,
:topic_edit => true,
:topic_lock => true
Role.create :id => 3,
:name => "超级版主",
:topic_delete => true,
:topic_digest => 3,
:topic_displayorder => 3,
:topic_highlight => true,
:topic_edit => true,
:topic_lock => true
Role.create :id => 4,
:name => "版主",
:topic_delete => true,
:topic_digest => 3,
:topic_displayorder => 3,
:topic_highlight => true,
:topic_edit => true,
:topic_lock => true
Role.create :id => 5,
:name => "普通用户"
=end
Role.create :id => 6,
:name => "游客"
end end
def self.down def self.down
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20111042872025) do ActiveRecord::Schema.define(:version => 20111042972029) do
create_table "boards", :force => true do |t| create_table "boards", :force => true do |t|
t.string "name", :default => "", :null => false t.string "name", :default => "", :null => false
...@@ -95,6 +95,16 @@ ActiveRecord::Schema.define(:version => 20111042872025) do ...@@ -95,6 +95,16 @@ ActiveRecord::Schema.define(:version => 20111042872025) do
t.datetime "updated_at" t.datetime "updated_at"
end end
create_table "roles", :force => true do |t|
t.string "name", :null => false
t.boolean "topic_delete", :default => false, :null => false
t.integer "topic_digest", :default => 0, :null => false
t.integer "topic_displayorder", :default => 0, :null => false
t.boolean "topic_highlight", :default => false, :null => false
t.boolean "topic_edit", :default => false, :null => false
t.boolean "topic_lock", :default => false, :null => false
end
create_table "settings", :primary_key => "name", :force => true do |t| create_table "settings", :primary_key => "name", :force => true do |t|
t.text "value" t.text "value"
end end
...@@ -119,27 +129,24 @@ ActiveRecord::Schema.define(:version => 20111042872025) do ...@@ -119,27 +129,24 @@ ActiveRecord::Schema.define(:version => 20111042872025) do
end end
create_table "users", :force => true do |t| create_table "users", :force => true do |t|
t.string "name", :null => false t.string "name", :null => false
t.string "nickname", :default => "", :null => false t.string "nickname", :default => "", :null => false
t.string "password", :null => false t.string "password", :null => false
t.string "email", :default => "", :null => false t.string "email", :default => "", :null => false
t.integer "usergroup_id", :default => 0, :null => false t.integer "role_id", :default => 0, :null => false
t.integer "admingroup_id", :default => 0, :null => false t.boolean "locked", :default => false, :null => false
t.boolean "locked", :default => false, :null => false t.string "regip", :default => "", :null => false
t.string "regip", :default => "", :null => false t.string "lastloginip", :default => "", :null => false
t.string "lastloginip", :default => "", :null => false t.integer "viewnum", :default => 0, :null => false
t.integer "readnum", :default => 0, :null => false t.integer "onlinetime", :default => 0, :null => false
t.integer "viewnum", :default => 0, :null => false t.integer "credit1", :default => 0, :null => false
t.integer "onlinetime", :default => 0, :null => false t.integer "credit2", :default => 0, :null => false
t.integer "credit", :default => 0, :null => false t.integer "credit3", :default => 0, :null => false
t.integer "credit1", :default => 0, :null => false t.integer "credit4", :default => 0, :null => false
t.integer "credit2", :default => 0, :null => false t.integer "credit5", :default => 0, :null => false
t.integer "credit3", :default => 0, :null => false t.integer "credit6", :default => 0, :null => false
t.integer "credit4", :default => 0, :null => false t.integer "credit7", :default => 0, :null => false
t.integer "credit5", :default => 0, :null => false t.integer "credit8", :default => 0, :null => false
t.integer "credit6", :default => 0, :null => false
t.integer "credit7", :default => 0, :null => false
t.integer "credit8", :default => 0, :null => false
t.string "locale" t.string "locale"
t.string "theme" t.string "theme"
t.datetime "created_at" t.datetime "created_at"
......
This diff is collapsed.
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
name: MyString
topic_delete: false
topic_digest: 1
topic_displayorder: 1
topic_highlight: false
topic_edit: false
topic_lock: false
two:
name: MyString
topic_delete: false
topic_digest: 1
topic_displayorder: 1
topic_highlight: false
topic_edit: false
topic_lock: false
require 'test_helper'
class RoleTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
1348 7748
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment