From 4df8ab6cc3c5dbcf0dcc3dafe15bb66af4660812 Mon Sep 17 00:00:00 2001 From: Matthew Fu Date: Mon, 16 Jan 2012 18:52:08 +0800 Subject: [PATCH] Add methods to work with manager role,add task to build user test data --- .../admin/module_apps_controller.rb | 73 +++++++++++++------ lib/tasks/user.rake | 29 ++++++++ 2 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 lib/tasks/user.rake diff --git a/app/controllers/admin/module_apps_controller.rb b/app/controllers/admin/module_apps_controller.rb index 34fcf1fd..129453b1 100644 --- a/app/controllers/admin/module_apps_controller.rb +++ b/app/controllers/admin/module_apps_controller.rb @@ -1,4 +1,7 @@ class Admin::ModuleAppsController < ApplicationController + before_filter :user_has_manager_privilege?, :only => [ :assign_manager, :remove_manager ] + before_filter :user_has_sub_manager_privilege?, :only => [ :assign_sub_manager, :remove_sub_manager ] + layout "admin" def index @@ -32,55 +35,77 @@ class Admin::ModuleAppsController < ApplicationController 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') + unless @assign_to_user.nil? + if @module_app.assign_sub_manager(@assign_to_user,current_user) + flash[:notice] = t('admin.app_auth.assigning_manager.add_sub_manager_ok') + else + flash[:notice] = t('admin.app_auth.assigning_manager.add_sub_manager_fail') + end 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" + flash[:notice] = t('admin.app_auth.assigning_manager.failed_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') + flash[:notice] = t('admin.app_auth.assigning_sub_manager.add_manager_ok') else - flash[:notice] = t('admin.app_auth.assign_fail_manager') + flash[:notice] = t('admin.app_auth.assigning_sub_manager.add_manager_fail') end else - flash[:notice] = t('admin.app_auth.assign_fail_manager_no_user') + flash[:notice] = t('admin.app_auth.assigning_sub_manager.failed_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') + flash[:notice] = t('admin.app_auth.delete_manager.success') else - flash[:notice] = t('admin.app_auth.delete_fail_manager') + flash[:notice] = t('admin.app_auth.delete_manager.fail') 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') + flash[:notice] = t('admin.app_auth.delete_sub_manager.success') else - flash[:notice] = t('admin.app_auth.delete_fail_sub_manager') + flash[:notice] = t('admin.app_auth.delete_sub_manager.fail') end redirect_to :action => "edit" end + + + private + def user_has_manager_privilege? + @module_app = ModuleApp.find(params[:id]) + @assign_to_user = User.find params[:manager_id] rescue nil + if current_user.admin? #only admin can assign app's manager + return + end + #user is not permited to do that + flash[:notice] = t('admin.app_auth.operation_not_permitted') + redirect_to :action => "edit" # [TODO] maybe need to redirect to some other page + end + + + 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 + return + end + #user is not permited to do that + flash[:notice] = t('admin.app_auth.operation_not_permitted') + redirect_to :action => "edit" # [TODO] maybe need to redirect to some other page + end + end \ No newline at end of file diff --git a/lib/tasks/user.rake b/lib/tasks/user.rake new file mode 100644 index 00000000..31ed7c24 --- /dev/null +++ b/lib/tasks/user.rake @@ -0,0 +1,29 @@ +# encoding: utf-8 +namespace :user do + + task :build => :environment do + User.all(conditions: {email: /nor/}).destroy_all + + username_list = %w{nor1 nor2 nor3 nor4 nor5 nor6 nor7} + userfirstname_list_en = %w{ One Two Thre For Fiv Six Sen } + userlastname_list_en = %w{ Aa Bb Cc Dd Ee Ff Gg } + + userfirstname_list_ct = %w{ 一一 二二 三三 四四 五五 六六 七七 } + userlastname_list_ct = %w{ 陳 林 吳 李 鄭 方 王 } + + first_name_field = AttributeField.first(conditions: {key: "first_name"}) + last_name_field = AttributeField.first(conditions: {key: "last_name"}) + major_field = AttributeField.first(conditions: {key: "major"}) + department_field = AttributeField.first(conditions: {key: "department"}) + + stud_role = Role.first(conditions: {key: 'student'}) + + username_list.each_with_index do |username,index| + user = User.create( :email => "#{username}@rulingcom.com", :password => 'password', :password_confirmation => 'password', :admin => false ,:role_id => stud_role.id,:sub_role_ids => [stud_role.sub_roles[Random.rand(stud_role.sub_roles.count-1)].id]) + AttributeValue.create( :user_id => user.id, :attribute_field_id => first_name_field.id, :key => 'first_name', :en => userfirstname_list_en[index], :zh_tw => userfirstname_list_ct[index] ) + AttributeValue.create( :user_id => user.id, :attribute_field_id => last_name_field.id, :key => 'last_name', :en => userlastname_list_en[index], :zh_tw => userlastname_list_ct[index] ) + AttributeValue.create( :user_id => user.id, :attribute_field_id => major_field.id, :key => 'major', :en => 'Information management', :zh_tw => '信息化管理' ) + AttributeValue.create( :user_id => user.id, :attribute_field_id => department_field.id, :key => 'department', :en => 'Computer Science', :zh_tw => '計算機科學' ) + end + end +end