1 | WAeUP portal authentication |
---|
2 | *************************** |
---|
3 | |
---|
4 | We need to protect most pieces of our portals from unauthenticated |
---|
5 | access. |
---|
6 | |
---|
7 | Therefore users have to login to access main functionality and they |
---|
8 | are able to log out afterwards. |
---|
9 | |
---|
10 | Before we can check access we have to create an app: |
---|
11 | |
---|
12 | >>> from zope.component.hooks import setSite # only needed in tests |
---|
13 | >>> from waeup.sirp.app import University |
---|
14 | >>> root = getRootFolder() |
---|
15 | >>> u = University() |
---|
16 | >>> root['app'] = u |
---|
17 | >>> setSite(root['app']) # only needed in tests |
---|
18 | |
---|
19 | To make sure, we can 'watch' pages, we first have to initialize our |
---|
20 | test browser: |
---|
21 | |
---|
22 | >>> from zope.testbrowser.testing import Browser |
---|
23 | >>> browser = Browser() |
---|
24 | >>> browser.handleErrors = False |
---|
25 | |
---|
26 | Creating users (principals) |
---|
27 | =========================== |
---|
28 | |
---|
29 | Before we can login, we have to provide a user (``principal`` in Zope |
---|
30 | terms) with a password (and optional a title or description): |
---|
31 | |
---|
32 | >>> root['app']['users'].addUser('bob', 'bobsecret', |
---|
33 | ... title='Bob', description='A sample user') |
---|
34 | |
---|
35 | We can also add complete `Account` objects. An `Account` stores the |
---|
36 | user credentials and some metadata persistently: |
---|
37 | |
---|
38 | >>> from waeup.sirp.authentication import Account |
---|
39 | >>> alice = Account('alice', 'alicesecret') |
---|
40 | >>> root['app']['users'].addAccount(alice) |
---|
41 | |
---|
42 | See ``users.txt`` for details about the UserContainer we use here. |
---|
43 | |
---|
44 | Users and local roles |
---|
45 | ===================== |
---|
46 | |
---|
47 | Accounts also hold infos about local roles assigned to a user. In the |
---|
48 | beginning, users have only the local owner role of their own account object: |
---|
49 | |
---|
50 | >>> alice.getLocalRoles() |
---|
51 | {'waeup.local.Owner': [<waeup.sirp.authentication.Account object at 0x...>]} |
---|
52 | |
---|
53 | We can tell an account, that Alice got some role for another object: |
---|
54 | |
---|
55 | >>> chalet = object() |
---|
56 | >>> root['app']['chalet'] = chalet |
---|
57 | >>> alice.notifyLocalRoleChanged(chalet, 'BigBoss', granted=True) |
---|
58 | |
---|
59 | Now Alice is the Big Boss: |
---|
60 | |
---|
61 | >>> alice.getLocalRoles() |
---|
62 | {'BigBoss': [<object object at 0x...>]} |
---|
63 | |
---|
64 | When we do not want Alice to be the Big Boss we can tell that too: |
---|
65 | |
---|
66 | >>> alice.notifyLocalRoleChanged(chalet, 'BigBoss', granted=False) |
---|
67 | >>> alice.getLocalRoles() |
---|
68 | {'waeup.local.Owner': [<waeup.sirp.authentication.Account object at 0x...>]} |
---|
69 | |
---|
70 | We can also use events to trigger such actions. This is recommended |
---|
71 | because we do not neccessarily know where Alice lives: |
---|
72 | |
---|
73 | >>> from waeup.sirp.users import LocalRoleSetEvent |
---|
74 | >>> from zope.event import notify |
---|
75 | >>> notify(LocalRoleSetEvent(chalet, 'BigBoss', 'alice', |
---|
76 | ... granted=True)) |
---|
77 | >>> alice.getLocalRoles() |
---|
78 | {'BigBoss': [<object object at 0x...>]} |
---|
79 | |
---|
80 | When objects are deleted, local roles are also deleted |
---|
81 | semi-magically. This happens through event subscribers listening to |
---|
82 | IObjectRemovedEvents. The latters are naturally only fired when ZODB |
---|
83 | stored objects are removed. Furthermore this subscriber reads the |
---|
84 | internal local roles table. |
---|
85 | |
---|
86 | We create a faculty and grant Bob a local role: |
---|
87 | |
---|
88 | >>> from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
89 | >>> from waeup.sirp.university.faculty import Faculty |
---|
90 | >>> faculty = Faculty() |
---|
91 | >>> root['app']['bobs_fac'] = faculty |
---|
92 | >>> role_manager = IPrincipalRoleManager(faculty) |
---|
93 | >>> role_manager.assignRoleToPrincipal( |
---|
94 | ... 'waeup.PortalManager', 'bob') |
---|
95 | |
---|
96 | We notify the machinery about that fact: |
---|
97 | |
---|
98 | >>> notify(LocalRoleSetEvent(faculty, 'waeup.PortalManager', 'bob', |
---|
99 | ... granted=True)) |
---|
100 | >>> bob = root['app']['users']['bob'] |
---|
101 | >>> bob.getLocalRoles() |
---|
102 | {'waeup.PortalManager': [<waeup.sirp...Faculty object at 0x...>]} |
---|
103 | |
---|
104 | When we delete the faculty from ZODB, also Bobs roles are modified: |
---|
105 | |
---|
106 | >>> del root['app']['bobs_fac'] |
---|
107 | >>> bob.getLocalRoles() |
---|
108 | {'waeup.local.Owner': [<waeup.sirp.authentication.Account object at 0x...>]} |
---|
109 | |
---|
110 | If one notifies the machinery of a local role removal for an object |
---|
111 | that cannot have local roles, this will cause no trouble: |
---|
112 | |
---|
113 | >>> mycontext = None #object() |
---|
114 | >>> notify(LocalRoleSetEvent( |
---|
115 | ... mycontext, 'waeup.PortalManager', 'bob', granted=False |
---|
116 | ... )) is None |
---|
117 | True |
---|
118 | |
---|
119 | When an account get deleted, also the local roles of the owner get |
---|
120 | removed. Let's setup a local role for `alice`: |
---|
121 | |
---|
122 | >>> faculty = Faculty() |
---|
123 | >>> root['app']['alice_fac'] = faculty |
---|
124 | >>> role_manager = IPrincipalRoleManager(faculty) |
---|
125 | >>> role_manager.assignRoleToPrincipal( |
---|
126 | ... 'waeup.PortalManager', 'alice') |
---|
127 | >>> notify(LocalRoleSetEvent(faculty, 'waeup.PortalManager', 'alice', |
---|
128 | ... granted=True)) |
---|
129 | |
---|
130 | The local role is set now: |
---|
131 | |
---|
132 | >>> from zope.securitypolicy.interfaces import IPrincipalRoleMap |
---|
133 | >>> IPrincipalRoleMap(faculty).getPrincipalsAndRoles() |
---|
134 | [('waeup.PortalManager', 'alice', PermissionSetting: Allow)] |
---|
135 | |
---|
136 | But when we delete Alices account from ZODB: |
---|
137 | |
---|
138 | >>> del root['app']['users']['alice'] |
---|
139 | >>> IPrincipalRoleMap(faculty).getPrincipalsAndRoles() |
---|
140 | [] |
---|
141 | |
---|
142 | the local role has gone. |
---|
143 | |
---|
144 | |
---|
145 | Logging in via side bar |
---|
146 | ======================= |
---|
147 | |
---|
148 | We can access the front page without restrictions: |
---|
149 | |
---|
150 | >>> browser.open('http://localhost/app') |
---|
151 | >>> print browser.headers['Status'] |
---|
152 | 200 Ok |
---|
153 | |
---|
154 | We have to go to one of the login pages first: |
---|
155 | |
---|
156 | >>> browser.open('http://localhost/app') |
---|
157 | >>> browser.getLink('Login').click() |
---|
158 | >>> print browser.headers['Status'] |
---|
159 | 200 Ok |
---|
160 | |
---|
161 | There is a login form on tis page: |
---|
162 | |
---|
163 | >>> 'form.login' in browser.contents |
---|
164 | True |
---|
165 | |
---|
166 | >>> 'form.logout' in browser.contents |
---|
167 | False |
---|
168 | |
---|
169 | We use this form: |
---|
170 | |
---|
171 | >>> browser.getControl(name='form.login').value = 'bob' |
---|
172 | >>> browser.getControl(name='form.password').value = 'invalidpw' |
---|
173 | >>> browser.getControl('Login').click() |
---|
174 | >>> 'You entered wrong credentials' in browser.contents |
---|
175 | True |
---|
176 | |
---|
177 | >>> browser.getControl(name='form.login').value = 'bob' |
---|
178 | >>> browser.getControl(name='form.password').value = 'bobsecret' |
---|
179 | >>> browser.getControl('Login').click() |
---|
180 | |
---|
181 | Now the login form is gone. Instead we have the opportunity to logout: |
---|
182 | |
---|
183 | >>> 'form.login' in browser.contents |
---|
184 | False |
---|
185 | |
---|
186 | >>> logout = browser.getLink('Logout') |
---|
187 | >>> logout |
---|
188 | <Link text='Logout' url='http://localhost/app/@@logout'> |
---|
189 | |
---|
190 | The user title is also displayed in the sidebar: |
---|
191 | |
---|
192 | >>> 'Bob' in browser.contents |
---|
193 | True |
---|
194 | |
---|
195 | We can also log out afterwards: |
---|
196 | |
---|
197 | >>> logout.click() |
---|
198 | >>> print browser.contents |
---|
199 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"... |
---|
200 | ...Login |
---|
201 | ... |
---|
202 | |
---|