From 18a82d7f472494d014e2a90f2e65726cb635a54a Mon Sep 17 00:00:00 2001 From: Matthew Fu Date: Fri, 13 Jan 2012 18:20:04 +0800 Subject: [PATCH] Manger and SubManager --- app/controllers/admin/app_auths_controller.rb | 6 +++ .../admin/module_apps_controller.rb | 53 +++++++++++++++++++ app/models/app_manager.rb | 12 +++++ app/models/module_app.rb | 37 +++++++++++++ app/models/user/user.rb | 3 ++ app/views/admin/module_apps/edit.html.erb | 41 ++++++++++++++ config/routes.rb | 5 ++ 7 files changed, 157 insertions(+) create mode 100644 app/models/app_manager.rb create mode 100644 app/views/admin/module_apps/edit.html.erb diff --git a/app/controllers/admin/app_auths_controller.rb b/app/controllers/admin/app_auths_controller.rb index e925b857..6cbbf62e 100644 --- a/app/controllers/admin/app_auths_controller.rb +++ b/app/controllers/admin/app_auths_controller.rb @@ -15,4 +15,10 @@ class Admin::AppAuthsController < ApplicationController @module_apps = ModuleApp.all end + def edit + @module_app = ModuleApp.find(params[:id]) + end + + + end \ No newline at end of file diff --git a/app/controllers/admin/module_apps_controller.rb b/app/controllers/admin/module_apps_controller.rb index 142830f1..34fcf1fd 100644 --- a/app/controllers/admin/module_apps_controller.rb +++ b/app/controllers/admin/module_apps_controller.rb @@ -18,6 +18,7 @@ class Admin::ModuleAppsController < ApplicationController end + def update @module_app = ModuleApp.find(params[:id]) unless params['module_app']['enable_frontend'].nil? @@ -30,4 +31,56 @@ class Admin::ModuleAppsController < ApplicationController format.js { render 'admin/attributes/toggle_enable' } end end + + def assign_sub_manager + @module_app = ModuleApp.find(params[:id]) + @assign_to_user = User.find params[:sub_manager_id] rescue nil + unless @assign_to_user.nil? + if @module_app.assign_sub_manager(@assign_to_user,current_user) + flash[:notice] = t('admin.app_auth.assign_success_sub_manager') + else + flash[:notice] = t('admin.app_auth.assign_fail_sub_manager') + end + else + flash[:notice] = t('admin.app_auth.assign_fail_sub_manager_no_user') + end + redirect_to :action => "edit" + end + + def assign_manager + @module_app = ModuleApp.find(params[:id]) + @assign_to_user = User.find params[:manager_id] rescue nil + unless @assign_to_user.nil? + if @module_app.assign_manager(@assign_to_user,current_user) + flash[:notice] = t('admin.app_auth.assign_success_manager') + else + flash[:notice] = t('admin.app_auth.assign_fail_manager') + end + else + flash[:notice] = t('admin.app_auth.assign_fail_manager_no_user') + end + redirect_to :action => "edit" + end + + def remove_manager + @module_app = ModuleApp.find(params[:id]) + @app_manager = AppManager.find(params[:app_manager_id]) rescue nil + if @module_app.remove_manager(@app_manager.user) + flash[:notice] = t('admin.app_auth.delete_success_manager') + else + flash[:notice] = t('admin.app_auth.delete_fail_manager') + end + redirect_to :action => "edit" + end + + def remove_sub_manager + @module_app = ModuleApp.find(params[:id]) + @app_sub_manager = AppManager.find(params[:app_sub_manager_id]) rescue nil + if @module_app.remove_sub_manager(@app_sub_manager.user) + flash[:notice] = t('admin.app_auth.delete_success_sub_manager') + else + flash[:notice] = t('admin.app_auth.delete_fail_sub_manager') + end + redirect_to :action => "edit" + end end \ No newline at end of file diff --git a/app/models/app_manager.rb b/app/models/app_manager.rb new file mode 100644 index 00000000..46cbbb91 --- /dev/null +++ b/app/models/app_manager.rb @@ -0,0 +1,12 @@ +class AppManager + include Mongoid::Document + include Mongoid::Timestamps + + belongs_to :user + + belongs_to :managing_app, :polymorphic => true #,:class_name => 'ModuleApp',:inverse_of => :managers,:foreign_key => "user_id" + belongs_to :sub_managing_app, :polymorphic => true #,:class_name => 'ModuleApp',:inverse_of => :sub_manager,:foreign_key => "sub_user_id" + + belongs_to :rule_creator,:class_name => 'User' + +end \ No newline at end of file diff --git a/app/models/module_app.rb b/app/models/module_app.rb index d398f7ef..029d55fa 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -14,9 +14,46 @@ class ModuleApp field :app_pages ,type: Array field :widgets ,type: Array + has_many :managers,as: :managing_app ,:class_name => "AppManager" #,:dependent => :destroy,:foreign_key => "managing_app_id",:inverse_of => :managing_app + has_many :sub_managers,as: :sub_managing_app ,:class_name => "AppManager"#, :dependent => :destroy,:foreign_key => "sub_managing_app_id",:inverse_of => :sub_managing_app + has_one :app_auth,dependent: :delete + + def assign_manager(user,assigner) + manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil + if manager.nil? + manager = self.managers.create(:user => user,:rule_creator => assigner) + end + manager + end + + def assign_sub_manager(user,assigner) + submanager = AppManager.first(conditions: {sub_managing_app_id: self.id,user_id: user.id}) rescue nil + if submanager.nil? + submanager = self.sub_managers.create(:user => user,:rule_creator => assigner) + end + submanager + end + + def remove_manager(user) + manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil + if manager + manager.destroy + else + false + end + end + + def remove_sub_manager(user) + submanager = AppManager.first(conditions: {sub_managing_app_id: self.id,user_id: user.id}) rescue nil + if submanager + submanager.destroy + else + false + end + end field :app_pages ,type: Array diff --git a/app/models/user/user.rb b/app/models/user/user.rb index 60199ce7..13d282bb 100644 --- a/app/models/user/user.rb +++ b/app/models/user/user.rb @@ -14,6 +14,9 @@ class User has_many :app_auths,as: :privilege_apps,:inverse_of => :privilege_lists has_many :blocked_apps, :inverse_of => :blocked_users, :class_name => "AppAuth" has_many :privilege_apps, :inverse_of => :privilege_users, :class_name => "AppAuth" + + has_many :managing_apps,:class_name => "AppManager" + belongs_to :role has_and_belongs_to_many :sub_roles accepts_nested_attributes_for :attribute_values, :allow_destroy => true diff --git a/app/views/admin/module_apps/edit.html.erb b/app/views/admin/module_apps/edit.html.erb new file mode 100644 index 00000000..ff967273 --- /dev/null +++ b/app/views/admin/module_apps/edit.html.erb @@ -0,0 +1,41 @@ +<% content_for :secondary do %> +<% end %> + + +
+
+
+ + +
+

<%= @module_app.title %>

+
+
Manager + <% @module_app.managers.each do |manager| %> +
<%= manager.user.name %> -AuthBy:<%= manager.rule_creator.name %> <%= link_to '[X]',remove_manager_admin_module_app_path(@module_app,manager),:method => :delete unless manager.user == current_user%>
+ <% end %> +
Add: + <%= form_tag(assign_manager_admin_module_app_path) do %> + <%= text_field_tag 'manager_id' %> + <%= submit_tag 'Add Manager' %> + <% end %> +
+
+ + +
+
Sub Manager + <% @module_app.sub_managers.each do |manager| %> +
<%= manager.user.name %> -AuthBy:<%= manager.rule_creator.name %> <%= link_to '[X]',remove_sub_manager_admin_module_app_path(@module_app,manager),:method => :delete unless manager.user == current_user%>
+ <% end %> +
Add: + <%= form_tag(assign_sub_manager_admin_module_app_path) do %> + <%= text_field_tag 'sub_manager_id' %> + <%= submit_tag 'Add Sub Manager' %> + <% end %> +
+
+ + +
+ diff --git a/config/routes.rb b/config/routes.rb index f2b4cc17..e0e9eec2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,6 +40,11 @@ PrototypeR4::Application.routes.draw do end resources :module_apps do member do + match 'assign_manager' ,:action=> 'assign_manager',:via => "post",:as =>:assign_manager + match 'assign_sub_manager' ,:action=> 'assign_sub_manager',:via => "post",:as =>:assign_sub_manager + + match 'remove_manager/:app_manager_id' ,:action=> 'remove_manager',:via => "delete",:as =>:remove_manager + match 'remove_sub_manager/:app_sub_manager_id' ,:action=> 'remove_sub_manager',:via => "delete",:as =>:remove_sub_manager get 'reload_frontend_pages' end end