2010年8月3日火曜日

acl tipfy

ちょっとCMS調べ等で寄り道をしたが、tipfyを使ったaclに話題を戻す。
はっきりいって、
http://www.tipfy.org/docs/api/tipfy.ext.acl.html
http://www.tipfy.org/wiki/extensions/acl/
の2カ所以外に資料はみつからない。あとは、ソースを読み解け、ということだと思う。
間違えているかもしれないが、現状思っていることを記録しておく。

[area]
webアプリケーションで範囲を設定して、それごとにaclを設定できるということのようだ。なぜなら、
user_acl = AclRules.get_by_area_and_user('my_area', 'user_2')
としてaclを取得してから、その役割に追加したり、調べたりできるようだからだ。
ここが分かりずらかった。大規模になったときには便利になるんだろう。

[rule]
# Set a dict of roles with an 'admin' role that has full access and
# assign users to it. Each role maps to a list of rules. Each rule is a
# tuple (topic, name, flag), where flag is as bool to allow or disallow
# access. Wildcard '*' can be used to match all topics and/or names.
とあるので、topic, name, flag によって構成されていることがわかる。

[role]
Groupではなく、roleと呼ぶみたいだ。

[roles_map]
Acl.roles_map = {
'admin': [
('*', '*', True),
],
}
roleとruleをタブルで列挙していく。

[使い方-ユーザーの追加]
# Assign users 'user_1' and 'user_2' to the 'admin' role.
AclRules.insert_or_update(area='my_area', user='user_1', roles=['admin'])
AclRules.insert_or_update(area='my_area', user='user_2', roles=['admin'])

[使い方-ユーザーに対する権限の追加/削除]
user_acl = AclRules.get_by_area_and_user('my_area', 'user_2')
user_acl.rules.append(('UserAdmin', '*', False))
user_acl.put()
area,userからAclRulesを取得して、そのrulesからappend()とかremove()すればよさそうだ。

[使い方-実装]
is_one(role)
is_any(roles)
is_all(roles)
has_any_access()
as_access(topic, name)
を使って実装していく。

[app-engine-site-creatorのACLの実装との比較について]
app-engine-site-creatorではACLの実装のかなりの部分をmodels.pyに記述している。
tipfyでもやり方によってはできるか?

[デコレータ]
Handlerを書くときにデコレータで
@login_required
@admin_required
のようにかけたら便利だな、と思っていたところ、
http://groups.google.com/group/tipfy/browse_thread/thread/cca740bf8e131510/c609e94e1e70fa96?lnk=gst&q=acl#c609e94e1e70fa96
に「書いたよ!」という人がいた。
一応、引用しておこう。

I wrote a function decorator for acl, it is according to my needs, but 
maybe it is interesting for others too: 
roles_map = { 
        # roles map definitions 
} 

# function decorator: 
def require(name, topic, area='whole'): 
    def wrap(func): 
        @auth.user_required 
        def decorated(*args, **kwargs): 
            context = {} 
            # user info for the template, getAuth is a custom function 
which 
            # returns username and the links generated by auth 
            #context['auth'] = getAuth() 
            #username = context['auth']['user'].username 
            username = auth.get_current_user().username 
            Acl.roles_map = roles_map 
            acl = Acl(area, username) 
            if acl.has_access(topic=topic, name=name): 
                return func(*args, **kwargs) 
            else: 
                context['errormessage'] = 'No access.' 
                return render_response('error.html', **context) 
        return decorated 
    return wrap 
# a functional form instead of a decorator, which return True or False 
instead of a response 
@auth.user_required 
def is_allowed(name, topic, area='whole'): 
    username = auth.get_current_user().username 
    Acl.roles_map = roles_map 
    acl = Acl(area, username) 
    return acl.has_access(topic=topic, name=name) 
Is there a more appropriate place to publish snippets and samples for 
tipfy? 
/ruben 


さて、そろそろ調査は終わりかな...

0 件のコメント:

コメントを投稿