A little bit better user handling
This commit is contained in:
parent
38a7ed4079
commit
ccd4218a4b
54
components/data/user.xml
Normal file
54
components/data/user.xml
Normal 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>
|
|
@ -17,8 +17,9 @@ sub Main()
|
||||||
|
|
||||||
' Confirm the configured server and user work
|
' Confirm the configured server and user work
|
||||||
m.user = AboutMe()
|
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"
|
print "Login failed, restart flow"
|
||||||
|
unset_setting("active_user")
|
||||||
goto start_login
|
goto start_login
|
||||||
end if
|
end if
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,10 @@ end function
|
||||||
function getJson(req)
|
function getJson(req)
|
||||||
'req.retainBodyOnError(True)
|
'req.retainBodyOnError(True)
|
||||||
'print req.GetToString()
|
'print req.GetToString()
|
||||||
|
data = req.GetToString()
|
||||||
|
if data = invalid or data = ""
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
json = ParseJson(req.GetToString())
|
json = ParseJson(req.GetToString())
|
||||||
return json
|
return json
|
||||||
end function
|
end function
|
||||||
|
@ -150,7 +154,7 @@ function authorize_request(request)
|
||||||
end if
|
end if
|
||||||
|
|
||||||
token = get_user_setting("token")
|
token = get_user_setting("token")
|
||||||
if token <> invalid and token <> "" then
|
if token <> invalid and token <> ""
|
||||||
auth = auth + ", Token=" + Chr(34) + token + Chr(34)
|
auth = auth + ", Token=" + Chr(34) + token + Chr(34)
|
||||||
end if
|
end if
|
||||||
|
|
||||||
|
|
|
@ -6,25 +6,28 @@ function get_token(user as String, password as String)
|
||||||
hashed_pass = digest.process(bytes)
|
hashed_pass = digest.process(bytes)
|
||||||
|
|
||||||
url = "Users/AuthenticateByName?format=json"
|
url = "Users/AuthenticateByName?format=json"
|
||||||
|
|
||||||
req = APIRequest(url)
|
req = APIRequest(url)
|
||||||
|
|
||||||
json = postJson(req, "Username=" + user + "&Password=" + hashed_pass)
|
json = postJson(req, "Username=" + user + "&Password=" + hashed_pass)
|
||||||
|
|
||||||
if json = invalid then return invalid
|
if json = invalid then return invalid
|
||||||
|
|
||||||
set_setting("active_user", json.User.id)
|
userdata = CreateObject("roSGNode", "UserData")
|
||||||
set_user_setting("id", json.User.id) ' redundant, but could come in handy
|
userdata.json = json
|
||||||
set_user_setting("token", json.AccessToken)
|
|
||||||
return json
|
userdata.callFunc("setActive")
|
||||||
|
userdata.callFunc("saveToRegistry")
|
||||||
|
return userdata
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function AboutMe()
|
function AboutMe()
|
||||||
url = Substitute("Users/{0}", get_setting("active_user"))
|
id = get_setting("active_user")
|
||||||
|
url = Substitute("Users/{0}", id)
|
||||||
resp = APIRequest(url)
|
resp = APIRequest(url)
|
||||||
return getJson(resp)
|
return getJson(resp)
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function SignOut()
|
function SignOut()
|
||||||
|
unset_user_setting("token")
|
||||||
unset_setting("active_user")
|
unset_setting("active_user")
|
||||||
end function
|
end function
|
||||||
|
|
Loading…
Reference in New Issue
Block a user