+ } else {
+ common_debug('access token "' . $at->tok . '" inserted', __FILE__);
+ // burn the old one
+ $orig_rt = clone($rt);
+ $rt->state = 2; // used
+ if (!$rt->update($orig_rt)) {
+ return null;
+ }
+ common_debug('request token "' . $rt->tok . '" updated', __FILE__);
+ }
+
+ return $at;
+ }
+
+ /*
+ * Add a new app user (Oauth_application_user) record
+ *
+ * @param Oauth_token_association $tokenAssoc token-to-app association
+ * @param Oauth_application $app the OAuth client app
+ * @param Token $at the access token
+ *
+ * @access private
+ *
+ * @return void
+ */
+ private function newAppUser($tokenAssoc, $app, $at)
+ {
+ $appUser = new Oauth_application_user();
+
+ $appUser->profile_id = $tokenAssoc->profile_id;
+ $appUser->application_id = $app->id;
+ $appUser->access_type = $app->access_type;
+ $appUser->token = $at->tok;
+ $appUser->created = common_sql_now();
+
+ $result = $appUser->insert();
+
+ if (!$result) {
+ common_log_db_error($appUser, 'INSERT', __FILE__);
+
+ // TRANS: Server error displayed when a database error occurs.
+ throw new Exception(
+ _('Database error inserting OAuth application user.')
+ );
+ }
+ }
+
+ /*
+ * Update an existing app user (Oauth_application_user) record
+ *
+ * @param Oauth_application_user $appUser existing app user rec
+ * @param Oauth_application $app the OAuth client app
+ * @param Token $at the access token
+ *
+ * @access private
+ *
+ * @return void
+ */
+ private function updateAppUser($appUser, $app, $at)
+ {
+ $original = clone($appUser);
+ $appUser->access_type = $app->access_type;
+ $appUser->token = $at->tok;
+
+ $result = $appUser->update($original);
+
+ if (!$result) {
+ common_log_db_error($appUser, 'UPDATE', __FILE__);
+ // TRANS: Server error displayed when a database error occurs.
+ throw new Exception(
+ _('Database error updating OAuth application user.')
+ );