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

Clear message


1 Peak Security

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

Let's save this code as security_test.py

1.1 Some sample code

    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 XXX import *
    4 
    5 myHouse = Building()
    6 Joe     = Owner()
    7 theInteraction = security.Interaction(user=Joe)
    8 
    9 allowed = theInteraction.allows(myHouse,"burnBuilding")
   10 if allowed:
   11     myHouse.burnBuiling()
   12 else:
   13     print allowed.message

== Now let's run this sample code ===

$ python security_test.py 
 
Burn baby burn 
 

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