1
|
|
|
from __future__ import absolute_import |
2
|
|
|
|
3
|
|
|
from ._connectorobject import ConnectorObject |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class User(ConnectorObject): |
7
|
|
|
def create(self, email, password, role="user", public=True, **kwargs): |
8
|
|
|
"""Creates the given user - using the passed in email and password. |
9
|
|
|
|
10
|
|
|
You can also set other default properties by passing in the relevant information:: |
11
|
|
|
|
12
|
|
|
usr.create("my@email","mypass",description="I like trains.") |
13
|
|
|
|
14
|
|
|
Furthermore, ConnectorDB permits immediate initialization of an entire user tree, |
15
|
|
|
so that you can create all relevant devices and streams in one go:: |
16
|
|
|
|
17
|
|
|
usr.create("my@email","mypass",devices={ |
18
|
|
|
"device1": { |
19
|
|
|
"nickname": "My train", |
20
|
|
|
"streams": { |
21
|
|
|
"stream1": { |
22
|
|
|
"schema": "{\"type\":\"string\"}", |
23
|
|
|
"datatype": "train.choochoo" |
24
|
|
|
} |
25
|
|
|
}, |
26
|
|
|
} |
27
|
|
|
}) |
28
|
|
|
|
29
|
|
|
The user and meta devices are created by default. If you want to add streams to the user device, |
30
|
|
|
use the "streams" option in place of devices in create. |
31
|
|
|
""" |
32
|
|
|
kwargs["email"] = email |
33
|
|
|
kwargs["password"] = password |
34
|
|
|
kwargs["role"] = role |
35
|
|
|
kwargs["public"] = public |
36
|
|
|
self.metadata = self.db.create( |
37
|
|
|
self.path, kwargs).json() |
38
|
|
|
|
39
|
|
|
def set_password(self, new_password): |
40
|
|
|
"""Sets a new password for the user""" |
41
|
|
|
self.set({"password": new_password}) |
42
|
|
|
|
43
|
|
View Code Duplication |
def devices(self): |
|
|
|
|
44
|
|
|
"""Returns the list of devices that belong to the user""" |
45
|
|
|
result = self.db.read(self.path, {"q": "ls"}) |
46
|
|
|
|
47
|
|
|
if result is None or result.json() is None: |
48
|
|
|
return [] |
49
|
|
|
devices = [] |
50
|
|
|
for d in result.json(): |
51
|
|
|
dev = self[d["name"]] |
52
|
|
|
dev.metadata = d |
53
|
|
|
devices.append(dev) |
54
|
|
|
return devices |
55
|
|
|
|
56
|
|
|
def __getitem__(self, device_name): |
57
|
|
|
"""Gets the child device by name""" |
58
|
|
|
return Device(self.db, self.path + "/" + device_name) |
59
|
|
|
|
60
|
|
|
def __repr__(self): |
61
|
|
|
"""Returns a string representation of the user""" |
62
|
|
|
return "[User:%s]" % (self.path, ) |
63
|
|
|
|
64
|
|
|
# ----------------------------------------------------------------------- |
65
|
|
|
# Following are getters and setters of the user's properties |
66
|
|
|
|
67
|
|
|
@property |
68
|
|
|
def email(self): |
69
|
|
|
"""gets the user's email address""" |
70
|
|
|
if "email" in self.data: |
71
|
|
|
return self.data["email"] |
72
|
|
|
return None |
73
|
|
|
|
74
|
|
|
@email.setter |
75
|
|
|
def email(self, new_email): |
76
|
|
|
"""sets the user's email address""" |
77
|
|
|
self.set({"email": new_email}) |
78
|
|
|
|
79
|
|
|
@property |
80
|
|
|
def public(self): |
81
|
|
|
"""gets whether the user is public |
82
|
|
|
(this means different things based on connectordb permissions setup - connectordb.com |
83
|
|
|
has this be whether the user is publically visible. Devices are individually public/private.) |
84
|
|
|
""" |
85
|
|
|
if "public" in self.data: |
86
|
|
|
return self.data["public"] |
87
|
|
|
return None |
88
|
|
|
|
89
|
|
|
@public.setter |
90
|
|
|
def public(self,new_public): |
91
|
|
|
"""Attempts to set whether the user is public""" |
92
|
|
|
self.set({"public": new_public}) |
93
|
|
|
|
94
|
|
|
@property |
95
|
|
|
def role(self): |
96
|
|
|
"""Gets the role of the user. This is the permissions level that the user has. It might |
97
|
|
|
not be accessible depending on the permissions setup of ConnectorDB. Returns None if not accessible""" |
98
|
|
|
if "role" in self.data: |
99
|
|
|
return self.data["role"] |
100
|
|
|
return None |
101
|
|
|
|
102
|
|
|
@role.setter |
103
|
|
|
def role(self,new_role): |
104
|
|
|
""" Attempts to set the user's role""" |
105
|
|
|
self.set({"role": new_role}) |
106
|
|
|
|
107
|
|
|
# The import has to go on the bottom because py3 imports are annoying |
108
|
|
|
from ._device import Device |
109
|
|
|
|