-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.py
134 lines (99 loc) · 3.77 KB
/
ring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Common code for schedule check tools
"""
from client import clients
def auth(username, password):
"""
Authenticate, returning a tuple of (access_token, refresh_token)
"""
UsernamePasswordAuth = clients.gateway.get_model('UsernamePasswordAuth')
AuthRequest = clients.gateway.get_model('AuthRequest')
username_password_auth = UsernamePasswordAuth(username=username,
password=password)
auth_request = AuthRequest(usernamePasswordAuth=username_password_auth)
response = clients.gateway.auth.authenticate(request=auth_request).result()
return (response.accessToken, response.refreshToken)
# This isn't used yet
def refresh_tokens(refresh_token):
"""
Given a refresh token, return a new tuple of (access_token, refresh_token)
This assumes that the refresh token is valid. Errors are not explicitly checked.
"""
response = clients.gateway.tokens.refresh(
refreshToken=refresh_token).result()
return (response.accessToken, response.refreshToken)
def get_overview(access_token):
"""
Return the overview for the user associated with the access token.
"""
sections = ["me", "systemInfo", "group",
"places", "lastKnowns", "users",
"controlsSettingsList", "devices"]
return clients.gateway.overview.getOverview(
accessToken=access_token,
sections=sections).result()
def get_user_by_name(users, name):
"""
Given a list of users, return the user with the matching name.
Return None if not found.
"""
return next((user for user in users if user.name.lower() == name.lower()), None)
def get_user_by_id(users, user_id):
"""
Given a list of users, return the user with the matching id.
Return None if not found.
"""
return next((user for user in users if user.id == user_id), None)
def get_member(members, user):
"""
Given a list of members, return the member that matches the user
Return None if not found
"""
return next((member for member in members if member.userId == user.id), None)
def is_parent(members, user):
"""
Given a list of members, is the given user a parent (admin)
"""
member = get_member(members, user)
if member:
return member.admin
return False
def is_child(members, user):
"""
Given a list of members, is the given user a child (managed)
"""
member = get_member(members, user)
if member:
return member.managed
return False
def update_controls_settings(access_token, group_id, user_id, block_all_internet):
"""
Selectively update controls settings for the specified user, who must be a
managed user.
The only setting actually supported (b/c that's all we need currently
here) is block internet.
"""
clients.gateway.controls.updateControlsSettings(
accessToken=access_token,
groupId=group_id,
userId=user_id,
request=dict(
blockAllInternet=block_all_internet,
predefinedPolicyIds=[], # required
customPolicies=[], # required
)).result()
def get_last_known_location(last_knowns, user_id):
"""
Given a list of last known objects for a group, return the last known
location for a given user id.
"""
last_known = next((lk for lk in last_knowns if lk.userId == user_id), None)
if not last_known:
return None
network = last_known.lastKnownNetworkLocation
device = last_known.lastKnownDeviceLocation
if not network and not device:
return None
network_time = network.observedTimestamp.timestamp() if network else 0
device_time = device.observedTimestamp.timestamp() if device else 0
return network if network_time > device_time else device