پرش به محتویات

۱۱. دسترسی به صورت کد

بسته‌ها و نقش‌هایی که یک پروژه همیشه دارد می‌توانند در یک فایل باشند، که مثل کد بازبینی می‌شود و روی همه‌ی سیستم‌ها یکی است. فایل فروشگاه:

"""
Who may do what in the shop, as code - applied by ``clearance_sync``, and by
every ``migrate`` (``SYNC_ON_MIGRATE``).

The file owns what it names: the admin site shows these bundles and roles
read only. Who is given a bundle or a role is not the file's to say - that is
done in the admin site, or by ``python manage.py example_data``.
"""

ACCESS = {
    'bundles': {
        # The orders of their own branch: read, edited and approved - never the price.
        'Branch managers': {
            'shop.order_view': 'branch',
            'shop.order_edit': 'branch',
            'shop.order_approve': 'branch',
            'shop.order_print_invoice': 'branch',
        },
        # Every order, with its price, and nothing changed.
        'Accountants': {
            'shop.order_view': 'all',
            'shop.order_view_price': None,
            'shop.order_print_invoice': 'all',
        },
        # Their own orders: read, and new ones placed for themselves.
        'Customers': {
            'shop.order_view': 'own',
            'shop.order_create': 'own',
        },
    },
    'roles': {
        # A customer starts with "Customers", and can never hold more than this.
        'customer': {
            'name': 'Customer',
            'ceiling': ['shop.order_view', 'shop.order_create'],
            'bundles': ['Customers'],
        },
        'manager': {
            'name': 'Branch manager',
            'ceiling': None,
            'bundles': ['Branch managers'],
        },
    },
}

تنظیمات اسم آن را می‌برند، و هر migrate آن را اعمال می‌کند:

CLEARANCE = {'ACCESS': 'config.access.ACCESS', 'SYNC_ON_MIGRATE': True}

یا با دست:

python manage.py clearance_sync --dry-run    # what would change, writing nothing
python manage.py clearance_sync --check      # in CI: fail when the database differs
python manage.py clearance_sync              # apply it, and say what changed

چه چیزی مال کیست

فایل صاحب چیزی است که نام می‌برد، و نه بیشتر. بسته یا نقشی که فایل نام می‌برد در پنل ادمین فقط‌خواندنی نشان داده می‌شود، با یادداشتی که همین را می‌گوید - صفحه‌ی نقش در قدم ۱۰ را ببینید. بسته‌ای که مدیر سیستم می‌سازد مال خود اوست، و فایل هیچ‌وقت به آن دست نمی‌زند.

اینکه بسته یا نقشی به چه کسی داده شود کار فایل نیست. این در پنل ادمین می‌ماند - یا، در فروشگاه، در example_data.

اکشن وضعیتِ مدلی که مجوزهای Django را نگه داشته هم در فایل می‌آید، زیر status_actions:

ACCESS = {
    'status_actions': {
        'shop.Payment': {'refund': {'field': 'state', 'to': ['refunded'], 'from': ['paid']}},
    },
}

فایلی که چیزی ناممکن را نام ببرد موقع بالا آمدن پروژه رد می‌شود - مجوزی که به شکل app_label.codename نوشته نشده، مدلی که نصب نیست، فیلد یا مقداری که مدلش ندارد (E001).