The PEAK Developers' Center   ShortIntroPeakSecurity UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Version as of 2003-12-05 21:52:36

Clear message


1 Peak Security

Here's a brief example of how to use peak security.

1.1 Some sample code

Let's save this code as security_test.py

    1 
    2 from peak.api import *
    3 
    4 class Owner(security.Permission):
    5     pass
    6 
    7 class User(object):
    8     pass
    9 
   10 class Building(object):
   11 
   12     security.allow(
   13         burnBuilding = Owner
   14         )
   15 
   16     def burnBuilding(self):
   17             print "Burn baby burn"
   18 
   19 class buildingRules(security.RuleSet):
   20 
   21     rules = Items(
   22           checkCanBurn = [Owner.of(Building)]
   23                   )
   24 
   25     def checkCanBurn(self, attempt):
   26         if attempt.user is attempt.subject.owner:
   27             return 1
   28         else:
   29             return security.Denial(
   30                  "You are not allowed to burn that building"
   31                                    )
   32 
   33 BuildingRules.declareRulesFor(security.IPermissionChecker)

1.2 How do we use this class

Let's save this code segment as security_test.py

    1 
    2 from peak.api import *
    3 from security_test import *
    4 
    5 myHouse = Building()
    6 Joe     = User()
    7 myHouse.owner = Joe
    8 theInteraction = security.Interaction(user=Joe)
    9 
   10 allowed = theInteraction.allows(myHouse,"burnBuilding")
   11 if allowed:
   12     myHouse.burnBuilding()
   13 else:
   14     print allowed.message

1.3 Now let's run this sample code

$ python security_test.py 
 
Burn baby burn 
 

1.4 A brief explanation

... to be continued

A detailed explanation of the concepts can be found here:

[WWW]http://www.eby-sarna.com/pipermail/peak/2003-December/000970.html


PythonPowered
EditText of this page (last modified 2003-12-05 21:52:36)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck