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

View File

@ -37,7 +37,7 @@ class Admin::ModuleAppsController < ApplicationController
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)
flash[:notice] = t('admin.app_auth.assigning_manager.add_sub_manager_ok')
else
@ -46,12 +46,13 @@ class Admin::ModuleAppsController < ApplicationController
else
flash[:notice] = t('admin.app_auth.assigning_manager.failed_no_user')
end
redirect_to :action => "edit"
flash[:notice] = t('admin.app_auth.can_not_add_this_user')
redirect_to :action => "edit"
end
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)
flash[:notice] = t('admin.app_auth.assigning_sub_manager.add_manager_ok')
else
@ -60,11 +61,13 @@ class Admin::ModuleAppsController < ApplicationController
else
flash[:notice] = t('admin.app_auth.assigning_sub_manager.failed_no_user')
end
redirect_to :action => "edit"
flash[:notice] = t('admin.app_auth.can_not_add_this_user')
redirect_to :action => "edit"
end
def remove_manager
@app_manager = AppManager.find(params[:app_manager_id])
if @module_app.remove_manager(@app_manager.user)
flash[:notice] = t('admin.app_auth.delete_manager.success')
else
@ -75,6 +78,7 @@ class Admin::ModuleAppsController < ApplicationController
def remove_sub_manager
@app_sub_manager = AppManager.find(params[:app_sub_manager_id])
if @module_app.remove_sub_manager(@app_sub_manager.user)
flash[:notice] = t('admin.app_auth.delete_sub_manager.success')
else
@ -100,7 +104,7 @@ class Admin::ModuleAppsController < ApplicationController
def user_has_sub_manager_privilege?
@module_app = ModuleApp.find(params[:id])
@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
end
#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,11 +19,17 @@ class ModuleApp
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)
manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil
if manager.nil?
if manager.nil?
manager = self.managers.create(:user => user,:rule_creator => assigner)
end
manager
@ -31,7 +37,7 @@ class ModuleApp
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?
if submanager.nil? && !self.managing_users.include?(user)
submanager = self.sub_managers.create(:user => user,:rule_creator => assigner)
end
submanager

View File

@ -12,11 +12,11 @@
<dl id="manager">
<dt>Manager</ht>
<% @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 %>
<dd>Add:
<%= 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' %>
<% end %>
</dd>
@ -26,11 +26,11 @@
<dl id="sub_manager">
<dt>Sub Manager</ht>
<% @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 %>
<dd>Add:
<%= 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' %>
<% end %>
</dd>

View File

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

View File

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