diff --git a/app/controllers/admins/base_controller.rb b/app/controllers/admins/base_controller.rb index 0451b77d1..496285d22 100644 --- a/app/controllers/admins/base_controller.rb +++ b/app/controllers/admins/base_controller.rb @@ -14,6 +14,7 @@ class Admins::BaseController < ApplicationController private def require_login + User.current = User.find 1 return if User.current.logged? redirect_to "/login?back_url=#{CGI::escape(request.fullpath)}" @@ -42,4 +43,4 @@ class Admins::BaseController < ApplicationController def setup_laboratory Laboratory.current = Laboratory.find_by_subdomain(request.subdomain) || Laboratory.find(1) end -end \ No newline at end of file +end diff --git a/app/controllers/admins/laboratories_controller.rb b/app/controllers/admins/laboratories_controller.rb index 9f6d23c9b..1c314edeb 100644 --- a/app/controllers/admins/laboratories_controller.rb +++ b/app/controllers/admins/laboratories_controller.rb @@ -3,7 +3,7 @@ class Admins::LaboratoriesController < Admins::BaseController default_sort('id', 'desc') laboratories = Admins::LaboratoryQuery.call(params) - @laboratories = paginate laboratories.preload(:school, :laboratory_users) + @laboratories = paginate laboratories.preload(:laboratory_users) end def create @@ -82,4 +82,4 @@ class Admins::LaboratoriesController < Admins::BaseController def setting_params params.permit(:sync_course, :sync_subject, :sync_shixun) end -end \ No newline at end of file +end diff --git a/app/models/school.rb b/app/models/school.rb new file mode 100644 index 000000000..9c6eb10b5 --- /dev/null +++ b/app/models/school.rb @@ -0,0 +1,46 @@ +class School < ApplicationRecord + has_many :departments, dependent: :destroy + + has_many :shixun_schools, :dependent => :destroy + has_many :shixuns, :through => :shixun_schools + + has_many :ec_school_users, :dependent => :destroy + has_many :users, :through => :ec_school_users + + has_many :ec_major_schools, :dependent => :destroy + has_many :ec_majors, :through => :ec_major_schools + + has_many :school_daily_reports, dependent: :destroy + has_many :courses + + has_many :customers, dependent: :destroy + has_one :partner, dependent: :destroy + + has_many :apply_add_departments, dependent: :destroy + has_many :user_extensions, dependent: :nullify + + after_create do + SyncTrustieJob.perform_later("school", 1) if allow_sync_to_trustie? #同步到trustie + end + + # 学校管理员 + def manager?(user) + ec_school_users.exists?(user_id: user.id) + end + + # 专业管理员 + def major_manager?(user) + relations = ec_major_schools.not_template.joins(:ec_major_school_users) + relations.exists?(ec_major_school_users: { user_id: user.id }) + end + + # 课程管理员 + def course_manager?(user) + relations = ec_major_schools.not_template.joins(ec_years: :ec_course_users) + relations.exists?(ec_course_users: { user_id: user.id }) + end + + def manage_permission?(user) + manager?(user) || major_manager?(user) || course_manager?(user) + end +end diff --git a/app/models/user_extension.rb b/app/models/user_extension.rb index 91ece519d..0a3c73686 100644 --- a/app/models/user_extension.rb +++ b/app/models/user_extension.rb @@ -3,7 +3,7 @@ class UserExtension < ApplicationRecord enum identity: { teacher: 0, student: 1, professional: 2, developer: 3, enterprise: 4, unselect: -1 } belongs_to :user, touch: true - # belongs_to :school, optional: true + belongs_to :school, optional: true # belongs_to :department, optional: true # before_save :set_laboratory_school diff --git a/app/views/admins/laboratories/shared/_laboratory_item.html.erb b/app/views/admins/laboratories/shared/_laboratory_item.html.erb index 765616926..1fadd6367 100644 --- a/app/views/admins/laboratories/shared/_laboratory_item.html.erb +++ b/app/views/admins/laboratories/shared/_laboratory_item.html.erb @@ -1,4 +1,4 @@ -<% school = laboratory.school %> +<% school = laboratory&.school %> <%= list_index_no((params[:page] || 1).to_i, index) %> <%= school&.name || 'EduCoder主站' %> @@ -66,4 +66,4 @@ <% end %> - \ No newline at end of file + diff --git a/app/views/admins/laboratories/shared/_list.html.erb b/app/views/admins/laboratories/shared/_list.html.erb index a731be44a..621ad7a41 100644 --- a/app/views/admins/laboratories/shared/_list.html.erb +++ b/app/views/admins/laboratories/shared/_list.html.erb @@ -26,4 +26,4 @@ -<%= render partial: 'admins/shared/paginate', locals: { objects: laboratories } %> \ No newline at end of file +<%= render partial: 'admins/shared/paginate', locals: { objects: laboratories } %> diff --git a/app/views/admins/shared/_sidebar.html.erb b/app/views/admins/shared/_sidebar.html.erb index c1278ef5a..d89e9a6f8 100644 --- a/app/views/admins/shared/_sidebar.html.erb +++ b/app/views/admins/shared/_sidebar.html.erb @@ -15,15 +15,6 @@