<s> from zope . interface import implements <EOL> from twisted . cred import portal , checkers , credentials <EOL> from nevow import inevow , rend , tags , guard , loaders <EOL> class MyPage ( rend . Page ) : <EOL> addSlash = True <EOL> docFactory = loaders . stan ( <EOL> tags . html [ <EOL> tags . head [ tags . title [ "<STR_LIT>" ] ] , <EOL> tags . body [ <EOL> tags . invisible ( render = tags . directive ( "<STR_LIT>" ) ) [ <EOL> tags . div ( pattern = "<STR_LIT:False>" ) [ <EOL> tags . form ( action = guard . LOGIN_AVATAR , method = '<STR_LIT>' ) [ <EOL> tags . table [ <EOL> tags . tr [ <EOL> tags . td [ "<STR_LIT>" ] , <EOL> tags . td [ tags . input ( type = '<STR_LIT:text>' , name = '<STR_LIT:username>' ) ] , <EOL> ] , <EOL> tags . tr [ <EOL> tags . td [ "<STR_LIT>" ] , <EOL> tags . td [ tags . input ( type = '<STR_LIT:password>' , name = '<STR_LIT:password>' ) ] , <EOL> ] <EOL> ] , <EOL> tags . input ( type = '<STR_LIT>' ) , <EOL> tags . p , <EOL> ] <EOL> ] , <EOL> tags . invisible ( pattern = "<STR_LIT:True>" ) [ <EOL> tags . h3 [ "<STR_LIT>" ] , <EOL> tags . invisible ( render = tags . directive ( "<STR_LIT>" ) ) , <EOL> tags . br , <EOL> tags . a ( href = guard . LOGOUT_AVATAR ) [ "<STR_LIT>" ] <EOL> ] <EOL> ] <EOL> ] <EOL> ] ) <EOL> def __init__ ( self , avatarId = None ) : <EOL> rend . Page . __init__ ( self ) <EOL> self . avatarId = avatarId <EOL> def render_isLogged ( self , context , data ) : <EOL> q = inevow . IQ ( context ) <EOL> true_pattern = q . onePattern ( '<STR_LIT:True>' ) <EOL> false_pattern = q . onePattern ( '<STR_LIT:False>' ) <EOL> if self . avatarId : return true_pattern or context . tag ( ) . clear ( ) <EOL> else : return false_pattern or context . tag ( ) . clear ( ) <EOL> def render_sessionId ( self , context , data ) : <EOL> sess = inevow . ISession ( context ) <EOL> return context . tag [ sess . uid ] <EOL> def logout ( self ) : <EOL> print "<STR_LIT>" <EOL> def noLogout ( ) : <EOL> return None <EOL> class MyRealm : <EOL> """<STR_LIT>""" <EOL> implements ( portal . IRealm ) <EOL> def requestAvatar ( self , avatarId , mind , * interfaces ) : <EOL> for iface in interfaces : <EOL> if iface is inevow . IResource : <EOL> if avatarId is checkers . ANONYMOUS : <EOL> resc = MyPage ( ) <EOL> resc . realm = self <EOL> return ( inevow . IResource , resc , noLogout ) <EOL> else : <EOL> resc = MyPage ( avatarId ) <EOL> resc . realm = self <EOL> return ( inevow . IResource , resc , resc . logout ) <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def createResource ( ) : <EOL> realm = MyRealm ( ) <EOL> porta = portal . Portal ( realm ) <EOL> myChecker = checkers . InMemoryUsernamePasswordDatabaseDontUse ( ) <EOL> myChecker . addUser ( "<STR_LIT:user>" , "<STR_LIT:password>" ) <EOL> myChecker . addUser ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> porta . registerChecker ( checkers . AllowAnonymousAccess ( ) , credentials . IAnonymous ) <EOL> porta . registerChecker ( myChecker ) <EOL> res = guard . SessionWrapper ( porta ) <EOL> return res </s>
