|
NAMEJifty::Manual::AccessControl_zhtw - 使用 Jifty 預設的 ACL 系統 DESCRIPTION以 Jifty 為基礎的應用程式都有存取權限控制 (ACL) 系統。這個系統能夠透過呼叫 "current_user_can" 函式來在建立、讀取、更新、刪除等動作之前自動驗證 Jifty::Record 物件的存取權限控制 ( ACL )。 在任何狀況,傳遞給 CRUD 程序的參數都會以額外的參數傳遞給 "current_user" 函式。 在 create() 函式,如果 current_user_can('create') 回傳 false 則拒絕此項操作。 在 _value() 或 "somefieldname" 函式, 如果 current_user_can('read') 回傳 false 則操作會被拒絕。 在 _set() 或 "set_somefieldname" 函式,如果 current_user_can('read') 回傳 false 則操作會被拒絕。 在 delete() 函式,如果 current_user_can('read') 回傳 false 則操作會被拒絕。 系統預設的行為會讓 "current_user_can" 回傳 1。 當你想檢查 ACLs 時,你需要在你的 "Jifty::Record" 內覆載 current_user_can() 函式。 It's likely that at some point, you'll decide you want to ask other questions on certain types of operations. Say, you only want to let administrators update the "paid_account" field. In that case, you'd override check_update_rights() to look for the "admin" right rather than the "update" right, if the "FIELD" is "paid_account". 透過插件來啟用權限控制 ( ENABLING ACCESS CONTROL USING THE USER PLUGIN )要無痛啟用 AccessControl 子系統,可以使用 User 插件來搭配一個驗證插件, 如 "Authentication::Password" 插件。我們可以在 etc/config.yml 來將這件事設定完成: Plugins:
- Authentication::Password: {}
然後,建立你的 "App::Model::User" 類別,讓它覆載 "Jifty::Plugin::User::Mixin::Model::User" 以及驗證插件的 "Jifty::Plugin::Authentication::Password::Mixin::Model::User" 舉例來說: use strict;
use warnings;
package App::Model::User;
use Jifty::DBI::Schema;
use App::Record schema {
};
use Jifty::Plugin::User::Mixin::Model::User;
use Jifty::Plugin::Authentication::Password::Mixin::Model::User;
# Your model-specific methods go here.
1;
下一步,使用 jifty 執行檔,來建立你資料庫內的資料表。如: $ bin/jifty schema --setup 延展資料模型管理 "User" 紀錄的資料模型並不限定於插件的定義。 它可以被延展,並可被定義其他額外的欄位。 每個欄位都會被新增到插件的欄位之中。 只需要簡單的在資料模型綱要 ( schema ) 的定義區塊中新增一些欄位定義: use Jifty::DBI::Schema;
use App::Record schema {
column 'extra_column_name';
column 'mygroup' =>
valid_values are qw/admin moderator user/,
default is 'user';
# more columns if necessary
};
關於定義資料模型綱要的完整語法,可於 Jifty::Manual::Models 或 Jifty::DBI::Schema 內參閱更詳細的資料。 如果你需要管理系統管理者群組,你需要保護這個群組欄位只能讓超級使用者 (superuser) 來改變。 接著,你需要覆載 "App::Model::User" 中的 "current_user_can" 函式: sub current_user_can {
my $self = shift;
my $type = shift;
my %args = (@_);
return 0
if ( $type eq 'update'
and !$self->current_user->is_superuser
and $args{'column'} eq 'mygroup' );
return 1;
}
在你的 "App::CurrentUser" 類別中,定義 "_init" 函式 讓你有機會新增更多資料到你的 "CurrentUser" 物件。 這則函式會在插件的 "_init" 函式完畢之後自動被呼叫。 package App::CurrentUser;
use strict;
use warnings;
use base qw(Jifty::CurrentUser);
__PACKAGE__->mk_accessors(qw(group));
sub _init {
my $self = shift;
my %args = (@_);
if (keys %args) {
$self->user_object(App::Model::User->new(current_user => $self));
$self->user_object->load_by_cols(%args);
if ( $self->user_object->mygroup eq 'admin') {
$self->is_superuser(1);
};
$self->group($self->user_object->mygroup);
};
$self->SUPER::_init(%args);
};
透過你所定義的 "App::CurrentUser" , 在 admin 群組內的使用者都將是超級使用者 (superuser) 並且你可以在你的應用程式內使用 "Jifty->web->current_user->group"。 "Authentication::Password" 插件所定義的樣板"Authentication::Password" 插件已經定義了許多可使用的樣板,來避免做重複的事情,
Doing checks at other places in your code如果你需要檢查更多關於資料模型為基礎的資料操作,那麼你需要自己撰寫 細節的程式碼。 "Jifty->web->current_user" 提供了 "App::CurrentUser" 物件以取得目前的使用者,此物件包含了一些相當好用的函式:
SEE ALSOJifty::CurrentUser, Jifty::Record, Jifty::RightsFrom, Jifty::Plugin::Authentication::Ldap, Jifty::Plugin::Authentication::CAS 翻譯者林佑安 (c9s) ( "cornelius.howl_at_gmail.com" ) <http://oulixe.us/>
|