source: main/waeup.ikoba/trunk/src/waeup/ikoba/doctests/authentication.txt @ 14292

Last change on this file since 14292 was 12993, checked in by Henrik Bettermann, 9 years ago

First adjustments for the upcoming Ikoba User Handbook.

File size: 4.4 KB
RevLine 
[12993]1Authentication
2**************
[4085]3
4We need to protect most pieces of our portals from unauthenticated
5access.
6
7Therefore users have to login to access main functionality and they
8are able to log out afterwards.
9
10Before we can check access we have to create an app:
11
[6180]12  >>> from zope.component.hooks import setSite # only needed in tests
[11954]13  >>> from waeup.ikoba.app import Company
[4085]14  >>> root = getRootFolder()
[11954]15  >>> u = Company()
[4085]16  >>> root['app'] = u
[6180]17  >>> setSite(root['app'])                     # only needed in tests
[4085]18
19To make sure, we can 'watch' pages, we first have to initialize our
20test browser:
21
22  >>> from zope.testbrowser.testing import Browser
23  >>> browser = Browser()
24  >>> browser.handleErrors = False
25
[12993]26Creating officers
27=================
[4085]28
[4092]29Before we can login, we have to provide a user (``principal`` in Zope
[4093]30terms) with a password (and optional a title or description):
[4092]31
[4744]32  >>> root['app']['users'].addUser('bob', 'bobsecret',
[4093]33  ...                           title='Bob', description='A sample user')
[4092]34
35We can also add complete `Account` objects. An `Account` stores the
36user credentials and some metadata persistently:
37
[11949]38  >>> from waeup.ikoba.authentication import Account
[7636]39  >>> alice = Account('alice', 'alicesecret',roles=['waeup.ManageDataCenter'])
[4744]40  >>> root['app']['users'].addAccount(alice)
[4092]41
[7173]42See ``userscontainer.txt`` for details about the UsersContainer we use here.
[4092]43
[12993]44Officers and local roles
45========================
[4093]46
[6180]47Accounts also hold infos about local roles assigned to a user. In the
[7173]48beginning, users have the local owner role of their own account object:
[6180]49
50  >>> alice.getLocalRoles()
[11949]51  {'waeup.local.Owner': [<waeup.ikoba.authentication.Account object at 0x...>]}
[6180]52
[11947]53User  automatically get the global ProductsOfficer role:
[7173]54
[7177]55  >>> alice.getSiteRolesForPrincipal()
[11947]56  ['waeup.ManageDataCenter', 'waeup.ProductsOfficer']
[7173]57
[7163]58We can tell an account, that Alice got some role for another object:
[6180]59
60  >>> chalet = object()
61  >>> root['app']['chalet'] = chalet
62  >>> alice.notifyLocalRoleChanged(chalet, 'BigBoss', granted=True)
63
64Now Alice is the Big Boss:
65
66  >>> alice.getLocalRoles()
67  {'BigBoss': [<object object at 0x...>]}
68
69When we do not want Alice to be the Big Boss we can tell that too:
70
71  >>> alice.notifyLocalRoleChanged(chalet, 'BigBoss', granted=False)
72  >>> alice.getLocalRoles()
[11949]73  {'waeup.local.Owner': [<waeup.ikoba.authentication.Account object at 0x...>]}
[6180]74
75We can also use events to trigger such actions. This is recommended
76because we do not neccessarily know where Alice lives:
77
[11949]78  >>> from waeup.ikoba.authentication import LocalRoleSetEvent
[6180]79  >>> from zope.event import notify
80  >>> notify(LocalRoleSetEvent(chalet, 'BigBoss', 'alice',
81  ...                          granted=True))
82  >>> alice.getLocalRoles()
83  {'BigBoss': [<object object at 0x...>]}
84
85When objects are deleted, local roles are also deleted
86semi-magically. This happens through event subscribers listening to
87IObjectRemovedEvents. The latters are naturally only fired when ZODB
88stored objects are removed. Furthermore this subscriber reads the
89internal local roles table.
90
91
[4092]92Logging in via side bar
93=======================
94
[4085]95We can access the front page without restrictions:
96
97  >>> browser.open('http://localhost/app')
98  >>> print browser.headers['Status']
99  200 Ok
100
[5404]101We have to go to one of the login pages first:
[4086]102
[6609]103  >>> browser.open('http://localhost/app')
[6685]104  >>> browser.getLink('Login').click()
[5404]105  >>> print browser.headers['Status']
106  200 Ok
107
108There is a login form on tis page:
109
[4086]110  >>> 'form.login' in browser.contents
111  True
112
[4093]113  >>> 'form.logout' in browser.contents
114  False
115
[4086]116We use this form:
117
[4093]118  >>> browser.getControl(name='form.login').value = 'bob'
[6609]119  >>> browser.getControl(name='form.password').value = 'invalidpw'
120  >>> browser.getControl('Login').click()
[8984]121  >>> 'You entered invalid credentials' in browser.contents
[6609]122  True
123
124  >>> browser.getControl(name='form.login').value = 'bob'
[4093]125  >>> browser.getControl(name='form.password').value = 'bobsecret'
[4086]126  >>> browser.getControl('Login').click()
[4092]127
[4093]128Now the login form is gone. Instead we have the opportunity to logout:
129
130  >>> 'form.login' in browser.contents
131  False
132
[4617]133  >>> logout = browser.getLink('Logout')
134  >>> logout
135  <Link text='Logout' url='http://localhost/app/@@logout'>
[4093]136
[4613]137The user title is also displayed in the sidebar:
[4093]138
[4613]139  >>> 'Bob' in browser.contents
[4094]140  True
[4093]141
[4094]142We can also log out afterwards:
143
[4617]144  >>> logout.click()
[5404]145  >>> print browser.contents
146  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
[6691]147  ...Login
[5404]148  ...
[4094]149
Note: See TracBrowser for help on using the repository browser.