[Zope-CVS] SVN: book/trunk/labyrinth/ Added code for "Spacesuits"
chapter.
Stephan Richter
srichter at cosmos.phy.tufts.edu
Wed Aug 25 10:02:27 EDT 2004
Log message for revision 27259:
Added code for "Spacesuits" chapter.
Changed:
A book/trunk/labyrinth/
A book/trunk/labyrinth/labyrinth.py
A book/trunk/labyrinth/labyrinth_security.py
-=-
Added: book/trunk/labyrinth/labyrinth.py
===================================================================
--- book/trunk/labyrinth/labyrinth.py 2004-08-25 14:01:53 UTC (rev 27258)
+++ book/trunk/labyrinth/labyrinth.py 2004-08-25 14:02:26 UTC (rev 27259)
@@ -0,0 +1,85 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Labyrinth Game
+
+This example of Zope 3's security was inspired by the
+zope.security.example. Unfortunately, this code did more than necessary and
+was therefore inappropriate for a book example. This is a much thinner version.
+
+$Id: labyrinth.py,v 1.1.1.1 2004/02/18 18:07:08 srichter Exp $
+"""
+class Person(object):
+
+ def __init__(self, id):
+ self.id = id
+ self.room = None
+
+ def goTo(self, direction):
+ assert direction in ('north', 'south', 'east', 'west'), \
+ '"%s" is not a valid direction' %direction
+ room = getattr(self.room, direction, None)
+ if room is None:
+ print 'There is no room %s of here.' %direction
+ else:
+ print room.description
+ self.room = room
+
+
+class Room(object):
+
+ def __init__(self, id, description):
+ self.id = id
+ self.description = description
+ self.north = self.south = self.east = self.west = None
+
+
+def setupWorld():
+ # Create the rooms
+ entrance = Room('entrance', 'The entrance of the labyrinth')
+ fork = Room('fork', 'The big decision. Do I go east or west.')
+ stairs = Room('stairs', 'Some long dark stairs.')
+ hall = Room('hall', 'A cathedral-like hall.')
+ corridor = Room('corridor', 'A long corridor')
+
+ # Connect the rooms
+ entrance.north = fork
+ fork.south, fork.west, fork.east = entrance, stairs, corridor
+ stairs.east, stairs.north = fork, hall
+ corridor.west, corridor.north = fork, hall
+ hall.west, hall.east = stairs, corridor
+
+ # Setup player
+ player = Person('player')
+ player.room = entrance
+ return player
+
+
+def main():
+ player = setupWorld()
+ command = ''
+ while command != 'exit':
+ try:
+ if command == 'info':
+ print player.room.description
+ elif command:
+ player.goTo(command)
+
+ except Exception, e:
+ print '%s: %s' %(e.__class__.__name__, e)
+
+ command = raw_input('Command: ')
+
+
+if __name__ == '__main__':
+ main()
Added: book/trunk/labyrinth/labyrinth_security.py
===================================================================
--- book/trunk/labyrinth/labyrinth_security.py 2004-08-25 14:01:53 UTC (rev 27258)
+++ book/trunk/labyrinth/labyrinth_security.py 2004-08-25 14:02:26 UTC (rev 27259)
@@ -0,0 +1,83 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Secure Labyrinth Simulation
+
+This example of Zope 3's security was inspired by the
+zope.security.example. Unfortunately, this code did more than necessary and
+was therefore inappropriate for a book example. This is a much thinner version.
+
+$Id: labyrinth_security.py,v 1.1.1.1 2004/02/18 18:07:08 srichter Exp $
+"""
+import labyrinth
+from zope.interface import implements
+from zope.security.interfaces import IParticipation
+from zope.security import checker, management, simplepolicies
+
+Allow = 'allow'
+
+permissions = {}
+
+def allowPerson(roomid, personid):
+ """Allow a particular person in a room."""
+ perms = permissions.setdefault(roomid, [])
+ perms.append(personid)
+
+
+class PersonParticipation(object):
+
+ implements(IParticipation)
+
+ def __init__(self, person):
+ self.principal = person
+ self.interaction = None
+
+
+class SecurityPolicy(simplepolicies.ParanoidSecurityPolicy):
+ """The Labyrinth's access security policy."""
+
+ def checkPermission(self, permission, object):
+ """See zope.security.interfaces.ISecurityPolicy"""
+ assert permission is Allow
+ allowed = permissions.get(object.id, [])
+ for participation in self.participations:
+ if not participation.principal.id in allowed:
+ return False
+ return True
+
+
+def setupSecurity(player):
+ # Setup security
+ management.setSecurityPolicy(SecurityPolicy)
+ room_checker = checker.NamesChecker(
+ ('description', 'north', 'south', 'west', 'east'), Allow)
+ checker.defineChecker(labyrinth.Room, room_checker)
+
+ # Allow the player everywhere but the corridor
+ allowPerson('entrance', player.id)
+ allowPerson('fork', player.id)
+ allowPerson('stairs', player.id)
+ allowPerson('hall', player.id)
+
+ # Add the player as a security manager and provide the player with a
+ # secure room
+ management.newInteraction(PersonParticipation(player))
+ proxied_room = checker.selectChecker(player.room).proxy(player.room)
+ player.room = proxied_room
+ return player
+
+
+if __name__ == '__main__':
+ oldSetupWorld = labyrinth.setupWorld
+ labyrinth.setupWorld = lambda : setupSecurity(oldSetupWorld())
+ labyrinth.main()
More information about the Zope-CVS
mailing list