First complete with manager and submanager

This commit is contained in:
Matthew Fu 2012-01-17 16:20:03 +08:00
parent 1dea9ba156
commit 9fa6131ad3
7 changed files with 52 additions and 15 deletions

View File

@ -1,7 +1,7 @@
class Admin::AppAuthsController < ApplicationController class Admin::AppAuthsController < ApplicationController
layout "admin" layout "admin"
before_filter :authenticate_user! before_filter :authenticate_user!
before_filter :is_admin? # before_filter :is_admin? ,:only => :index
def index def index
# @roles = Role.all.entries # @roles = Role.all.entries
@ -12,7 +12,11 @@ class Admin::AppAuthsController < ApplicationController
# obj_auth = obj.send "auth" # obj_auth = obj.send "auth"
# [:app_obj => app,:auth_field => obj_auth] # [:app_obj => app,:auth_field => obj_auth]
# end # end
if current_user.admin?
@module_apps = ModuleApp.all @module_apps = ModuleApp.all
else
@module_apps = current_user.managing_apps.collect{|t| t.managing_app}
end
end end
def edit def edit

View File

@ -37,7 +37,7 @@ class Admin::ModuleAppsController < ApplicationController
def assign_sub_manager def assign_sub_manager
unless @assign_to_user.nil? unless @assign_to_user.nil? || @assign_to_user.admin?
if @module_app.assign_sub_manager(@assign_to_user,current_user) if @module_app.assign_sub_manager(@assign_to_user,current_user)
flash[:notice] = t('admin.app_auth.assigning_manager.add_sub_manager_ok') flash[:notice] = t('admin.app_auth.assigning_manager.add_sub_manager_ok')
else else
@ -46,12 +46,13 @@ class Admin::ModuleAppsController < ApplicationController
else else
flash[:notice] = t('admin.app_auth.assigning_manager.failed_no_user') flash[:notice] = t('admin.app_auth.assigning_manager.failed_no_user')
end end
flash[:notice] = t('admin.app_auth.can_not_add_this_user')
redirect_to :action => "edit" redirect_to :action => "edit"
end end
def assign_manager def assign_manager
unless @assign_to_user.nil? unless @assign_to_user.nil? || @assign_to_user.admin?
if @module_app.assign_manager(@assign_to_user,current_user) if @module_app.assign_manager(@assign_to_user,current_user)
flash[:notice] = t('admin.app_auth.assigning_sub_manager.add_manager_ok') flash[:notice] = t('admin.app_auth.assigning_sub_manager.add_manager_ok')
else else
@ -60,11 +61,13 @@ class Admin::ModuleAppsController < ApplicationController
else else
flash[:notice] = t('admin.app_auth.assigning_sub_manager.failed_no_user') flash[:notice] = t('admin.app_auth.assigning_sub_manager.failed_no_user')
end end
flash[:notice] = t('admin.app_auth.can_not_add_this_user')
redirect_to :action => "edit" redirect_to :action => "edit"
end end
def remove_manager def remove_manager
@app_manager = AppManager.find(params[:app_manager_id])
if @module_app.remove_manager(@app_manager.user) if @module_app.remove_manager(@app_manager.user)
flash[:notice] = t('admin.app_auth.delete_manager.success') flash[:notice] = t('admin.app_auth.delete_manager.success')
else else
@ -75,6 +78,7 @@ class Admin::ModuleAppsController < ApplicationController
def remove_sub_manager def remove_sub_manager
@app_sub_manager = AppManager.find(params[:app_sub_manager_id])
if @module_app.remove_sub_manager(@app_sub_manager.user) if @module_app.remove_sub_manager(@app_sub_manager.user)
flash[:notice] = t('admin.app_auth.delete_sub_manager.success') flash[:notice] = t('admin.app_auth.delete_sub_manager.success')
else else
@ -100,7 +104,7 @@ class Admin::ModuleAppsController < ApplicationController
def user_has_sub_manager_privilege? def user_has_sub_manager_privilege?
@module_app = ModuleApp.find(params[:id]) @module_app = ModuleApp.find(params[:id])
@assign_to_user = User.find params[:sub_manager_id] rescue nil @assign_to_user = User.find params[:sub_manager_id] rescue nil
if current_user.admin? || @module_app.managers.include?(current_user) #admin or app's manager can assign app's subanager if current_user.admin? || @module_app.managing_users.include?(current_user) #admin or app's manager can assign app's subanager
return return
end end
#user is not permited to do that #user is not permited to do that

View File

@ -0,0 +1,19 @@
module Admin::ModuleAppHelper
def if_permit_to_delete(item)
case item.downcase
when :manager
current_user.admin?
when :sub_manager
@module_app.managing_users.include?(current_user) || current_user.admin?
end
end
def if_permit_to_assign(item)
if_permit_to_delete(item)
end
def get_auth_by(manager_obj)
"-AuthBy: " +( manager_obj.rule_creator==current_user ? t('me') : manager_obj.rule_creator.name)
end
end

View File

@ -19,7 +19,13 @@ class ModuleApp
has_one :app_auth,dependent: :delete has_one :app_auth,dependent: :delete
def managing_users
self.managers.collect{ |t| t.user }
end
def sub_managing_users
self.sub_managers.collect{ |t| t.user }
end
def assign_manager(user,assigner) def assign_manager(user,assigner)
manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil
@ -31,7 +37,7 @@ class ModuleApp
def assign_sub_manager(user,assigner) def assign_sub_manager(user,assigner)
submanager = AppManager.first(conditions: {sub_managing_app_id: self.id,user_id: user.id}) rescue nil submanager = AppManager.first(conditions: {sub_managing_app_id: self.id,user_id: user.id}) rescue nil
if submanager.nil? if submanager.nil? && !self.managing_users.include?(user)
submanager = self.sub_managers.create(:user => user,:rule_creator => assigner) submanager = self.sub_managers.create(:user => user,:rule_creator => assigner)
end end
submanager submanager

View File

@ -12,11 +12,11 @@
<dl id="manager"> <dl id="manager">
<dt>Manager</ht> <dt>Manager</ht>
<% @module_app.managers.each do |manager| %> <% @module_app.managers.each do |manager| %>
<dd><%= 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%> </dd> <dd><%= manager.user.name %> <%= get_auth_by(manager) %> <%= link_to '[X]',remove_manager_admin_module_app_path(@module_app,manager),:method => :delete if if_permit_to_delete(:manager) && manager.user != current_user %> </dd>
<% end %> <% end %>
<dd>Add: <dd>Add:
<%= form_tag(assign_manager_admin_module_app_path) do %> <%= form_tag(assign_manager_admin_module_app_path) do %>
<%= text_field_tag 'manager_id' %> <%= text_field_tag 'manager_id','Enter User ID here',:disabled => !if_permit_to_assign(:manager)%>
<%= submit_tag 'Add Manager' %> <%= submit_tag 'Add Manager' %>
<% end %> <% end %>
</dd> </dd>
@ -26,11 +26,11 @@
<dl id="sub_manager"> <dl id="sub_manager">
<dt>Sub Manager</ht> <dt>Sub Manager</ht>
<% @module_app.sub_managers.each do |manager| %> <% @module_app.sub_managers.each do |manager| %>
<dd><%= 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%> </dd> <dd><%= manager.user.name %> -AuthBy:<%= manager.rule_creator.name %> <%= link_to '[X]',remove_sub_manager_admin_module_app_path(@module_app,manager),:method => :delete if if_permit_to_delete(:sub_manager) && manager.user != current_user %> </dd>
<% end %> <% end %>
<dd>Add: <dd>Add:
<%= form_tag(assign_sub_manager_admin_module_app_path) do %> <%= form_tag(assign_sub_manager_admin_module_app_path) do %>
<%= text_field_tag 'sub_manager_id' %> <%= text_field_tag 'sub_manager_id','Enter User ID here',:disabled => !if_permit_to_assign(:sub_manager)%>
<%= submit_tag 'Add Sub Manager' %> <%= submit_tag 'Add Sub Manager' %>
<% end %> <% end %>
</dd> </dd>

View File

@ -5,6 +5,8 @@ en:
_locale: English _locale: English
me: Me
add: Add add: Add
back: Back back: Back
create: Create create: Create

View File

@ -2,6 +2,8 @@ zh_tw:
_locale: 中文 _locale: 中文
me:
add: 新增 add: 新增
back: 回去 back: 回去
create: 創造 create: 創造