From d87b52bedd524086c8efad93f803c743aad58c43 Mon Sep 17 00:00:00 2001 From: nwb Date: Wed, 4 Jun 2014 09:19:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E6=88=90=E5=91=98=E8=BF=81?= =?UTF-8?q?=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/course.rb | 2 +- app/models/user.rb | 20 +++++++++++++++++++ ...0140603033359_add_boards_type_to_boards.rb | 1 + ...3081801_add_courseid_to_enabled_modules.rb | 3 ++- ...140604011630_add_courseid_to_to_members.rb | 15 ++++++++++++++ db/schema.rb | 3 ++- lib/redmine.rb | 12 +++++------ lib/redmine/access_control.rb | 8 +++++++- 8 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20140604011630_add_courseid_to_to_members.rb diff --git a/app/models/course.rb b/app/models/course.rb index 0df09dd37..b611261cc 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -81,7 +81,7 @@ class Course < ActiveRecord::Base perm = Redmine::AccessControl.permission(permission) base_statement = (perm && perm.read? ? "#{Course.table_name}.status <> #{Course::STATUS_ARCHIVED}" : "#{Course.table_name}.status = #{Course::STATUS_ACTIVE}") if perm && perm.course_module - base_statement << " AND #{Course.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')" + base_statement << " AND #{Course.table_name}.id IN (SELECT em.course_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.course_module}')" end if options[:course] course_statement = "#{Course.table_name}.id = #{options[:course].id}" diff --git a/app/models/user.rb b/app/models/user.rb index edf703873..d376cd858 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -276,6 +276,7 @@ class User < Principal def reload(*args) @name = nil @projects_by_role = nil + @courses_by_role = nil @membership_by_project_id = nil base_reload(*args) end @@ -634,6 +635,25 @@ class User < Principal @projects_by_role end + # 课程的角色权限 + def courses_by_role + return @courses_by_role if @courses_by_role + + @courses_by_role = Hash.new([]) + memberships.each do |membership| + if membership.course + membership.roles.each do |role| + @courses_by_role[role] = [] unless @courses_by_role.key?(role) + @courses_by_role[role] << membership.course + end + end + end + @courses_by_role.each do |role, courses| + courses.uniq! + end + + @courses_by_role + end # Returns true if user is arg or belongs to arg def is_or_belongs_to?(arg) if arg.is_a?(User) diff --git a/db/migrate/20140603033359_add_boards_type_to_boards.rb b/db/migrate/20140603033359_add_boards_type_to_boards.rb index 598f5b517..76193b642 100644 --- a/db/migrate/20140603033359_add_boards_type_to_boards.rb +++ b/db/migrate/20140603033359_add_boards_type_to_boards.rb @@ -6,6 +6,7 @@ class AddBoardsTypeToBoards < ActiveRecord::Migration project = Project.find_by_id(board.project_id) if project && project.project_type == 1 board.course_id = board.project_id + board.project_id = nil board.save end end diff --git a/db/migrate/20140603081801_add_courseid_to_enabled_modules.rb b/db/migrate/20140603081801_add_courseid_to_enabled_modules.rb index cefc38258..baa1ef81d 100644 --- a/db/migrate/20140603081801_add_courseid_to_enabled_modules.rb +++ b/db/migrate/20140603081801_add_courseid_to_enabled_modules.rb @@ -5,7 +5,8 @@ class AddCourseidToEnabledModules < ActiveRecord::Migration EnabledModule.all.each do |enablemodule| project = Project.find_by_id(enablemodule.project_id) if project && project.project_type == 1 - enablemodule.course_id = board.project_id + enablemodule.course_id = enablemodule.project_id + enablemodule.project_id = nil enablemodule.save end end diff --git a/db/migrate/20140604011630_add_courseid_to_to_members.rb b/db/migrate/20140604011630_add_courseid_to_to_members.rb new file mode 100644 index 000000000..14111c1b9 --- /dev/null +++ b/db/migrate/20140604011630_add_courseid_to_to_members.rb @@ -0,0 +1,15 @@ +class AddCourseidToToMembers < ActiveRecord::Migration + def change + add_column :members, :course_id, :int + + # 课程成员数据迁移 + Member.all.each do |member| + project = Project.find_by_id(member.project_id) + if project && project.project_type == 1 + member.course_id = member.project_id + member.project_id = nil + member.save + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9178151a7..34629bc09 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140603033360) do +ActiveRecord::Schema.define(:version => 20140603081801) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -315,6 +315,7 @@ ActiveRecord::Schema.define(:version => 20140603033360) do create_table "enabled_modules", :force => true do |t| t.integer "project_id" t.string "name", :null => false + t.integer "course_id" end add_index "enabled_modules", ["project_id"], :name => "enabled_modules_project_id" diff --git a/lib/redmine.rb b/lib/redmine.rb index 7703b2b2b..ff2dd2bb3 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -98,6 +98,12 @@ Redmine::AccessControl.map do |map| map.permission :add_subprojects, {:projects => [:new, :create]}, :require => :member map.permission :view_journals_for_messages, {:gantts => [:show, :update]}, :read => true + #课程权限模块 + map.course_module :course do + map.permission :view_course, {:courses => [:show], :activities => [:index]}, :public => true, :read => true + map.permission :view_courses,{:courses => [:course]},:read => true + end + map.project_module :issue_tracking do |map| # Issue categories map.permission :manage_categories, {:projects => :settings, :issue_categories => [:index, :show, :new, :create, :edit, :update, :destroy]}, :require => :member @@ -178,12 +184,6 @@ Redmine::AccessControl.map do |map| map.permission :manage_related_issues, {:repositories => [:add_related_issue, :remove_related_issue]} end - #课程权限模块 - map.project_module :course do - map.permission :view_course, {:courses => [:show], :activities => [:index]}, :public => true, :read => true - map.permission :view_courses,{:courses => [:course]},:read => true - end - #作业模块权限 map.project_module :bids do |map| map.permission :view_homework_attaches, {:bids => [:show, :show_project, :revision]}, :read => true diff --git a/lib/redmine/access_control.rb b/lib/redmine/access_control.rb index 7c4a5a5ec..0158a05ea 100644 --- a/lib/redmine/access_control.rb +++ b/lib/redmine/access_control.rb @@ -76,6 +76,7 @@ module Redmine class Mapper def initialize @project_module = nil + @course_module = nil end def permission(name, hash, options={}) @@ -90,13 +91,18 @@ module Redmine @project_module = nil end + def course_module(name, options={}) + @course_module = name + yield self + @course_module = nil + end def mapped_permissions @permissions end end class Permission - attr_reader :name, :actions, :project_module + attr_reader :name, :actions, :project_module ,:course_module def initialize(name, hash, options) @name = name