The PEAK Developers' Center   Diff for "ShortIntroPeakSecurity" UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Ignore changes in the amount of whitespace

Differences between version dated 2003-12-05 21:17:07 and 2005-01-07 12:29:11 (spanning 26 versions)

Deletions are marked like this.
Additions are marked like this.

= Peak Security =
 
== A short intro to peak security ==
Here's a brief example of how to use peak security. (A more detailed explanation with multiple examples can be found on the SecurityRules page.)
 
 
== Some sample code ==
 
Let's save this code as security_test.py
 
{{{
#!python
from peak.api import *
 
class Owner(security.Permission):
    pass
 
class User(object):
    pass
 
class Building(object):
 
    binding.metadata(
        burnBuilding = Owner # permission needed for the method
    )
 
    def burnBuilding(self):
        print "Burn baby burn"
 
 
class BuildingRules(security.Context):
 
    [security.hasPermission.when("perm==Owner and isinstance(subject,Building)")]
    def checkCanBurn(self, user, perm, subject):
        return user is subject.owner or security.Denial(
            "You are not allowed to burn that building"
        )
 
}}}
 
== How do we use this class ==
 
Let's save this code segment as security_test_runner.py
 
{{{
#!python
from peak.api import *
from security_test import *
 
myHouse = Building()
Joe = User()
myHouse.owner = Joe
theContext = BuildingRules()
 
permissionNeeded = theContext.permissionFor(myHouse,"burnBuilding")
allowed = theContext.hasPermission(Joe, permissionNeeded, myHouse)
 
print "Joe is an owner he should be allowed to burn his own house:"
if allowed:
    myHouse.burnBuilding()
else:
    print allowed.message
 
print "Now we 'remove' ownership from Joe and try to burn it again"
 
myHouse.owner = None
allowed = theContext.hasPermission(Joe, permissionNeeded, myHouse )
if allowed:
    myHouse.burnBuilding()
else:
    print allowed.message
 
}}}
 
 
== Now let's run this sample code ==
 
{{{
$ python security_test_runner.py
Joe is an owner he should be allowed to burn his own house:
Burn baby burn
Now we 'remove' ownership from Joe and try to burn it again
You are not allowed to burn that building
}}}
 
 
== A brief explanation ==
 
... to be continued
 
A detailed explanation of the concepts can be found here (but they describe an older implementation of peak.security; please see SecurityRules for up-to-date documentation):
 
[http://www.eby-sarna.com/pipermail/peak/2003-December/000970.html]
 
[http://www.eby-sarna.com/pipermail/peak/2003-October/000842.html]
 

PythonPowered
ShowText of this page
EditText of this page
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck