5555
5656-- [[ CREDENTIAL TABLE MANAGEMENT ]] --
5757
58- -- Add a user and its credential together. Existing entries at either index are left untouched
59- -- by the underlying table add. Returns the result status of the credential add.
60- -- A user is only created if the credential add succeeded, so a failed or colliding credential never leaves an orphan user .
58+ -- Add a user and its credential together, so a user never exists without a credential.
59+ -- The credential is added first; the user is only added if that succeeded, so a failed or
60+ -- colliding credential add leaves no user behind. Returns the result status of the credential add .
6161function lock_utils .add_user_and_credential (device , args )
6262 local name = " Guest " .. args .userIndex
6363 local user_type = " guest"
@@ -77,10 +77,29 @@ function lock_utils.add_user_and_credential(device, args)
7777 return credential_status
7878end
7979
80+ -- Apply the arguments of an updateCredential command to the credentials table.
81+ -- Returns the command result status and the credential's userIndex.
82+ function lock_utils .update_commanded_credential (device , credential_args )
83+ local existing = tables .find_entry (device , " credentials" , credential_args .credentialIndex )
84+ if not existing then return consts .COMMAND_RESULT .FAILURE end
85+ local updates = {}
86+ if credential_args .credentialName ~= nil and credential_args .credentialName ~= existing .credentialName then
87+ updates .credentialName = credential_args .credentialName
88+ end
89+ if credential_args .userIndex ~= nil and credential_args .userIndex ~= existing .userIndex then
90+ updates .userIndex = credential_args .userIndex
91+ end
92+ if next (updates ) then
93+ local status = tables .update_entry (device , " credentials" , credential_args .credentialIndex , updates )
94+ if status ~= consts .COMMAND_RESULT .SUCCESS then return status end
95+ end
96+ return consts .COMMAND_RESULT .SUCCESS , updates .userIndex or existing .userIndex
97+ end
98+
8099-- Per command: update the tables after the lock confirmed a set-credential command we sent.
81- -- Returns the command result status, or nil for any other command. The user is always new,
82- -- and its index is chosen here. Also returns the newly chosen user's userIndex
83- function lock_utils .add_commanded_credential (device , command_in_progress , credential_args )
100+ -- Returns the command result status, or nil for any other command, plus the affected userIndex.
101+ -- For addCredential the user is always new, and its index is chosen here.
102+ function lock_utils .apply_commanded_credential (device , command_in_progress , credential_args )
84103 if command_in_progress == consts .LOCK_CREDENTIALS .ADD then
85104 local user_index = tables .next_index (device , " users" )
86105 local status = lock_utils .add_user_and_credential (device , {
@@ -89,20 +108,19 @@ function lock_utils.add_commanded_credential(device, command_in_progress, creden
89108 credentialType = credential_args .credentialType ,
90109 credentialName = credential_args .credentialName ,
91110 })
92- return status , user_index
111+ return status , status == consts . COMMAND_RESULT . SUCCESS and user_index or nil
93112 elseif command_in_progress == consts .LOCK_CREDENTIALS .UPDATE then
94- return consts . COMMAND_RESULT . SUCCESS , credential_args . userIndex
113+ return lock_utils . update_commanded_credential ( device , credential_args )
95114 end
96115end
97116
98- -- Per report: add a user and PIN credential for a code the lock reports at `credential_index` without any command
99- -- from us (sync, or a programming event). If a credential already exists there it is left in place.
117+ -- Per report: ensure a user and PIN credential exist for a code the lock reports at `credential_index`
118+ -- without any command from us (sync, or a programming event). If a credential already exists there it
119+ -- is left in place
100120function lock_utils .add_reported_credential (device , credential_index )
101- local found_cred = tables .find_entry (device , " credentials" , credential_index )
102- if found_cred then return end
103- local user_index = tables .next_index (device , " users" )
121+ if tables .find_entry (device , " credentials" , credential_index ) then return end
104122 lock_utils .add_user_and_credential (device , {
105- userIndex = user_index ,
123+ userIndex = tables . next_index ( device , " users " ) ,
106124 credentialIndex = credential_index ,
107125 })
108126end
0 commit comments