1 | Users Container |
---|
2 | *************** |
---|
3 | |
---|
4 | .. :doctest: |
---|
5 | .. :layer: waeup.ikoba.testing.IkobaUnitTestLayer |
---|
6 | |
---|
7 | Before we can start, we need some password managers available: |
---|
8 | |
---|
9 | >>> from zope.app.authentication.placelesssetup import ( |
---|
10 | ... PlacelessSetup) |
---|
11 | >>> PlacelessSetup().setUp() |
---|
12 | |
---|
13 | We can create a user container which will hold the useraccounts for |
---|
14 | us: |
---|
15 | |
---|
16 | >>> from waeup.ikoba.userscontainer import UsersContainer |
---|
17 | >>> myusers = UsersContainer() |
---|
18 | |
---|
19 | We can add users, just by passing a name, a password, and (optionally) |
---|
20 | a title and a description: |
---|
21 | |
---|
22 | >>> myusers.addUser('bob', 'bobssecret') |
---|
23 | |
---|
24 | Now, Bob is in the container: |
---|
25 | |
---|
26 | >>> list(myusers) |
---|
27 | [u'bob'] |
---|
28 | |
---|
29 | We can get Bob's account: |
---|
30 | |
---|
31 | >>> bob = myusers['bob'] |
---|
32 | >>> bob |
---|
33 | <waeup.ikoba.authentication.Account object at 0x...> |
---|
34 | |
---|
35 | >>> bob.name |
---|
36 | 'bob' |
---|
37 | |
---|
38 | >>> bob.title |
---|
39 | 'bob' |
---|
40 | |
---|
41 | >>> bob.description |
---|
42 | |
---|
43 | |
---|
44 | As we did not give a title, the name was taken instead |
---|
45 | for title (but not for description). The password, however, is stored encoded: |
---|
46 | |
---|
47 | >>> bob.password |
---|
48 | '{SSHA}...' |
---|
49 | |
---|
50 | We can export user accounts: |
---|
51 | |
---|
52 | >>> from waeup.ikoba.userscontainer import UserExporter |
---|
53 | >>> import os |
---|
54 | >>> import tempfile |
---|
55 | >>> workdir = tempfile.mkdtemp() |
---|
56 | >>> outfile = os.path.join(workdir, 'myoutput.csv') |
---|
57 | >>> exporter = UserExporter() |
---|
58 | >>> site = {'users':myusers} |
---|
59 | >>> exporter.export_all(site, outfile) |
---|
60 | >>> result = open(outfile, 'rb').read() |
---|
61 | >>> 'name,title,public_name,description,email,phone,roles,local_roles,password' in result |
---|
62 | True |
---|
63 | >>> '{SSHA}' in result |
---|
64 | True |
---|
65 | |
---|
66 | We can delete users: |
---|
67 | |
---|
68 | >>> myusers.delUser('alice') |
---|
69 | |
---|
70 | The container won't complain, although there is no ``alice`` stored |
---|
71 | yet. But we can really delete users: |
---|
72 | |
---|
73 | >>> myusers.delUser('bob') |
---|
74 | >>> list(myusers) |
---|
75 | [] |
---|
76 | |
---|
77 | Clean up: |
---|
78 | |
---|
79 | >>> import shutil |
---|
80 | >>> shutil.rmtree(workdir) |
---|