[12951] | 1 | Authentication |
---|
| 2 | ************** |
---|
[4085] | 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 | |
---|
[6180] | 12 | >>> from zope.component.hooks import setSite # only needed in tests |
---|
[7811] | 13 | >>> from waeup.kofa.app import University |
---|
[4085] | 14 | >>> root = getRootFolder() |
---|
| 15 | >>> u = University() |
---|
| 16 | >>> root['app'] = u |
---|
[6180] | 17 | >>> setSite(root['app']) # only needed in tests |
---|
[4085] | 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 | |
---|
[12951] | 26 | Creating officers |
---|
| 27 | ================= |
---|
[4085] | 28 | |
---|
[4092] | 29 | Before we can login, we have to provide a user (``principal`` in Zope |
---|
[4093] | 30 | terms) with a password (and optional a title or description): |
---|
[4092] | 31 | |
---|
[15287] | 32 | >>> root['app']['users'].addUser('bob', 'bobSecret1', |
---|
[4093] | 33 | ... title='Bob', description='A sample user') |
---|
[4092] | 34 | |
---|
| 35 | We can also add complete `Account` objects. An `Account` stores the |
---|
| 36 | user credentials and some metadata persistently: |
---|
| 37 | |
---|
[7811] | 38 | >>> from waeup.kofa.authentication import Account |
---|
[7636] | 39 | >>> alice = Account('alice', 'alicesecret',roles=['waeup.ManageDataCenter']) |
---|
[4744] | 40 | >>> root['app']['users'].addAccount(alice) |
---|
[4092] | 41 | |
---|
[7173] | 42 | See ``userscontainer.txt`` for details about the UsersContainer we use here. |
---|
[4092] | 43 | |
---|
[12951] | 44 | Officers and local roles |
---|
| 45 | ======================== |
---|
[4093] | 46 | |
---|
[6180] | 47 | Accounts also hold infos about local roles assigned to a user. In the |
---|
[7173] | 48 | beginning, users have the local owner role of their own account object: |
---|
[6180] | 49 | |
---|
| 50 | >>> alice.getLocalRoles() |
---|
[7811] | 51 | {'waeup.local.Owner': [<waeup.kofa.authentication.Account object at 0x...>]} |
---|
[6180] | 52 | |
---|
[7636] | 53 | User automatically get the global AcademicsOfficer role: |
---|
[7173] | 54 | |
---|
[7177] | 55 | >>> alice.getSiteRolesForPrincipal() |
---|
[7636] | 56 | ['waeup.ManageDataCenter', 'waeup.AcademicsOfficer'] |
---|
[7173] | 57 | |
---|
[7163] | 58 | We 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 | |
---|
| 64 | Now Alice is the Big Boss: |
---|
| 65 | |
---|
| 66 | >>> alice.getLocalRoles() |
---|
| 67 | {'BigBoss': [<object object at 0x...>]} |
---|
| 68 | |
---|
| 69 | When 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() |
---|
[7811] | 73 | {'waeup.local.Owner': [<waeup.kofa.authentication.Account object at 0x...>]} |
---|
[6180] | 74 | |
---|
| 75 | We can also use events to trigger such actions. This is recommended |
---|
| 76 | because we do not neccessarily know where Alice lives: |
---|
| 77 | |
---|
[7811] | 78 | >>> from waeup.kofa.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 | |
---|
| 85 | When objects are deleted, local roles are also deleted |
---|
| 86 | semi-magically. This happens through event subscribers listening to |
---|
| 87 | IObjectRemovedEvents. The latters are naturally only fired when ZODB |
---|
| 88 | stored objects are removed. Furthermore this subscriber reads the |
---|
| 89 | internal local roles table. |
---|
| 90 | |
---|
| 91 | We create a faculty and grant Bob a local role: |
---|
| 92 | |
---|
| 93 | >>> from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[7811] | 94 | >>> from waeup.kofa.university.faculty import Faculty |
---|
[6180] | 95 | >>> faculty = Faculty() |
---|
| 96 | >>> root['app']['bobs_fac'] = faculty |
---|
| 97 | >>> role_manager = IPrincipalRoleManager(faculty) |
---|
| 98 | >>> role_manager.assignRoleToPrincipal( |
---|
| 99 | ... 'waeup.PortalManager', 'bob') |
---|
| 100 | |
---|
| 101 | We notify the machinery about that fact: |
---|
| 102 | |
---|
| 103 | >>> notify(LocalRoleSetEvent(faculty, 'waeup.PortalManager', 'bob', |
---|
| 104 | ... granted=True)) |
---|
| 105 | >>> bob = root['app']['users']['bob'] |
---|
| 106 | >>> bob.getLocalRoles() |
---|
[7811] | 107 | {'waeup.PortalManager': [<waeup.kofa...Faculty object at 0x...>]} |
---|
[6180] | 108 | |
---|
| 109 | When we delete the faculty from ZODB, also Bobs roles are modified: |
---|
| 110 | |
---|
| 111 | >>> del root['app']['bobs_fac'] |
---|
| 112 | >>> bob.getLocalRoles() |
---|
[7811] | 113 | {'waeup.local.Owner': [<waeup.kofa.authentication.Account object at 0x...>]} |
---|
[6180] | 114 | |
---|
[6202] | 115 | If one notifies the machinery of a local role removal for an object |
---|
| 116 | that cannot have local roles, this will cause no trouble: |
---|
| 117 | |
---|
| 118 | >>> mycontext = None #object() |
---|
| 119 | >>> notify(LocalRoleSetEvent( |
---|
| 120 | ... mycontext, 'waeup.PortalManager', 'bob', granted=False |
---|
| 121 | ... )) is None |
---|
| 122 | True |
---|
| 123 | |
---|
[6203] | 124 | When an account get deleted, also the local roles of the owner get |
---|
| 125 | removed. Let's setup a local role for `alice`: |
---|
| 126 | |
---|
| 127 | >>> faculty = Faculty() |
---|
| 128 | >>> root['app']['alice_fac'] = faculty |
---|
| 129 | >>> role_manager = IPrincipalRoleManager(faculty) |
---|
| 130 | >>> role_manager.assignRoleToPrincipal( |
---|
| 131 | ... 'waeup.PortalManager', 'alice') |
---|
| 132 | >>> notify(LocalRoleSetEvent(faculty, 'waeup.PortalManager', 'alice', |
---|
| 133 | ... granted=True)) |
---|
| 134 | |
---|
| 135 | The local role is set now: |
---|
| 136 | |
---|
| 137 | >>> from zope.securitypolicy.interfaces import IPrincipalRoleMap |
---|
| 138 | >>> IPrincipalRoleMap(faculty).getPrincipalsAndRoles() |
---|
| 139 | [('waeup.PortalManager', 'alice', PermissionSetting: Allow)] |
---|
| 140 | |
---|
| 141 | But when we delete Alices account from ZODB: |
---|
| 142 | |
---|
| 143 | >>> del root['app']['users']['alice'] |
---|
| 144 | >>> IPrincipalRoleMap(faculty).getPrincipalsAndRoles() |
---|
| 145 | [] |
---|
| 146 | |
---|
[9308] | 147 | the local role has gone and also the site roles have been removed: |
---|
[6203] | 148 | |
---|
[9308] | 149 | >>> prm = IPrincipalRoleManager(root['app']) |
---|
| 150 | >>> [x[0] for x in prm.getRolesForPrincipal('alice') |
---|
| 151 | ... if x[0].startswith('waeup.')] |
---|
| 152 | [] |
---|
[6203] | 153 | |
---|
[9308] | 154 | |
---|
[4092] | 155 | Logging in via side bar |
---|
| 156 | ======================= |
---|
| 157 | |
---|
[4085] | 158 | We can access the front page without restrictions: |
---|
| 159 | |
---|
| 160 | >>> browser.open('http://localhost/app') |
---|
| 161 | >>> print browser.headers['Status'] |
---|
| 162 | 200 Ok |
---|
| 163 | |
---|
[5404] | 164 | We have to go to one of the login pages first: |
---|
[4086] | 165 | |
---|
[6609] | 166 | >>> browser.open('http://localhost/app') |
---|
[6685] | 167 | >>> browser.getLink('Login').click() |
---|
[5404] | 168 | >>> print browser.headers['Status'] |
---|
| 169 | 200 Ok |
---|
| 170 | |
---|
| 171 | There is a login form on tis page: |
---|
| 172 | |
---|
[4086] | 173 | >>> 'form.login' in browser.contents |
---|
| 174 | True |
---|
| 175 | |
---|
[4093] | 176 | >>> 'form.logout' in browser.contents |
---|
| 177 | False |
---|
| 178 | |
---|
[4086] | 179 | We use this form: |
---|
| 180 | |
---|
[4093] | 181 | >>> browser.getControl(name='form.login').value = 'bob' |
---|
[6609] | 182 | >>> browser.getControl(name='form.password').value = 'invalidpw' |
---|
| 183 | >>> browser.getControl('Login').click() |
---|
[8984] | 184 | >>> 'You entered invalid credentials' in browser.contents |
---|
[6609] | 185 | True |
---|
| 186 | |
---|
| 187 | >>> browser.getControl(name='form.login').value = 'bob' |
---|
[15287] | 188 | >>> browser.getControl(name='form.password').value = 'bobSecret1' |
---|
[4086] | 189 | >>> browser.getControl('Login').click() |
---|
[4092] | 190 | |
---|
[4093] | 191 | Now the login form is gone. Instead we have the opportunity to logout: |
---|
| 192 | |
---|
| 193 | >>> 'form.login' in browser.contents |
---|
| 194 | False |
---|
| 195 | |
---|
[4617] | 196 | >>> logout = browser.getLink('Logout') |
---|
| 197 | >>> logout |
---|
| 198 | <Link text='Logout' url='http://localhost/app/@@logout'> |
---|
[4093] | 199 | |
---|
[4613] | 200 | The user title is also displayed in the sidebar: |
---|
[4093] | 201 | |
---|
[4613] | 202 | >>> 'Bob' in browser.contents |
---|
[4094] | 203 | True |
---|
[4093] | 204 | |
---|
[4094] | 205 | We can also log out afterwards: |
---|
| 206 | |
---|
[4617] | 207 | >>> logout.click() |
---|
[5404] | 208 | >>> print browser.contents |
---|
| 209 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"... |
---|
[6691] | 210 | ...Login |
---|
[5404] | 211 | ... |
---|
[4094] | 212 | |
---|