A little bit better user handling

This commit is contained in:
Nick Bisby 2019-04-20 12:40:06 -05:00
parent 38a7ed4079
commit ccd4218a4b
No known key found for this signature in database
GPG Key ID: F6E0C4E6D0B5EB36
4 changed files with 70 additions and 8 deletions

54
components/data/user.xml Normal file
View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="UserData" extends="ContentNode">
<interface>
<field id="id" type="string" />
<field id="username" type="string" />
<field id="token" type="string" />
<field id="json" type="associativearray" onChange="setDataFromJSON" />
<function name="getPreference" />
<function name="setPreference" />
<function name="loadFromRegistry" />
<function name="saveToRegistry" />
<function name="setActive" />
</interface>
<script type="text/brightscript" uri="pkg:/source/utils/config.brs" />
<script type="text/brightscript">
<![CDATA[
sub setDataFromJSON()
json = m.top.json
loadFromJSON(json)
end sub
function loadFromJSON(json)
m.top.id = json.User.id
m.top.username = json.User.username
m.top.token = json.AccessToken
end function
function loadFromRegistry(id as String)
m.top.id = id
m.top.username = get_user_setting("username")
m.top.token = get_user_setting("token")
end function
function saveToRegistry()
set_user_setting("username", m.top.username)
set_user_setting("token", m.top.token)
end function
function getPreference(key as String, default as String)
return get_user_setting("pref-" + key, default)
end function
function setPreference(key as String, value as String)
return set_user_setting("pref-" + key, value)
end function
function setActive()
set_setting("active_user", m.top.id)
end function
]]>
</script>
</component>

View File

@ -17,8 +17,9 @@ sub Main()
' Confirm the configured server and user work
m.user = AboutMe()
if m.user.id <> get_setting("active_user")
if m.user = invalid or m.user.id <> get_setting("active_user")
print "Login failed, restart flow"
unset_setting("active_user")
goto start_login
end if

View File

@ -59,6 +59,10 @@ end function
function getJson(req)
'req.retainBodyOnError(True)
'print req.GetToString()
data = req.GetToString()
if data = invalid or data = ""
return invalid
end if
json = ParseJson(req.GetToString())
return json
end function
@ -150,7 +154,7 @@ function authorize_request(request)
end if
token = get_user_setting("token")
if token <> invalid and token <> "" then
if token <> invalid and token <> ""
auth = auth + ", Token=" + Chr(34) + token + Chr(34)
end if

View File

@ -6,25 +6,28 @@ function get_token(user as String, password as String)
hashed_pass = digest.process(bytes)
url = "Users/AuthenticateByName?format=json"
req = APIRequest(url)
json = postJson(req, "Username=" + user + "&Password=" + hashed_pass)
if json = invalid then return invalid
set_setting("active_user", json.User.id)
set_user_setting("id", json.User.id) ' redundant, but could come in handy
set_user_setting("token", json.AccessToken)
return json
userdata = CreateObject("roSGNode", "UserData")
userdata.json = json
userdata.callFunc("setActive")
userdata.callFunc("saveToRegistry")
return userdata
end function
function AboutMe()
url = Substitute("Users/{0}", get_setting("active_user"))
id = get_setting("active_user")
url = Substitute("Users/{0}", id)
resp = APIRequest(url)
return getJson(resp)
end function
function SignOut()
unset_user_setting("token")
unset_setting("active_user")
end function