function restapi_tokens_del($id) { global $db; //delete token $sql = 'DELETE FROM restapi_tokens WHERE id = ?'; $ret = $db->query($sql, array($id)); db_e($ret); $sql = 'DELETE FROM restapi_token_details WHERE token_id = ?'; $ret = $db->query($sql, array($id)); db_e($ret); //delete user mapping restapi_user_del_token($id); return ''; }
function restapi_user_set_token($user, $token) { global $db; //first, ensure no other user are associated with this token restapi_user_del_token($token); //then update $sql = 'INSERT INTO restapi_token_user_mapping (user, token_id) VALUES (?, ?)'; $ret = $db->query($sql, array($user, $token)); db_e($ret); return true; }
public function usermanUpdateUser($id, $display, $data) { if (!function_exists('restapi_user_get_user_tokens') && function_exists('restapi_tokens_put')) { return ''; } if ($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'user') { foreach ($_POST as $key => $data) { if (preg_match('/^restapi_(\\d*)_token_status/i', $key, $matches)) { $token_status = $_POST[$matches[0]]; if (isset($token_status)) { if ($token_status == "enabled") { $this->userman->setModuleSettingByID($id, 'restapi', 'restapi_token_status', true); } elseif ($token_status == "disabled") { $this->userman->setModuleSettingByID($id, 'restapi', 'restapi_token_status', false); } else { $inherit = true; $this->userman->setModuleSettingByID($id, 'restapi', 'restapi_token_status', null); } } } } } $tokens = \restapi_user_get_user_tokens($id); $userdata = $this->userman->getUserByID($id); $enabled = $this->userman->getCombinedModuleSettingByID($id, 'restapi', 'restapi_token_status'); if (!$enabled) { if (!empty($tokens)) { foreach ($tokens as $token) { restapi_user_del_token($token); } } return; } //Coming from the Extensions or Users Page if ($display == 'extensions' || $display == 'users') { //Only Generate a token if we have no tokens if (empty($tokens)) { $tokendata = \restapi_tokens_get(); $tokendata['assoc_user'] = $id; $tokendata['token'] = \restapi_tokens_generate(); $tokendata['tokenkey'] = \restapi_tokens_generate(); $tokendata['modules'] = array('*'); $tokendata['users'] = array('*'); $tokendata['token_status'] = 'enabled'; $tokendata['name'] = 'User ' . $userdata['username'] . ' (autogen)'; $tokendata['desc'] = 'Autogenerated token on new user creation'; \restapi_tokens_put($tokendata); } //Coming from User Manager } elseif ($display == 'userman') { $settings = array(); foreach ($_POST as $key => $data) { if (preg_match('/^restapi_(\\d*)_(.*)/i', $key, $matches)) { if ($matches[2] == "token_status") { $settings[$matches[1]][$matches[2]] = $data == "enabled" ? "enabled" : "disabled"; } else { $settings[$matches[1]][$matches[2]] = $data; } } } $defaultExt = $userdata['default_extension'] != "none" ? array($userdata['default_extension']) : array(); //If tokens exist then update each token if (!empty($tokens)) { foreach ($tokens as $token) { $tokendata = \restapi_tokens_get($token); $tid = $tokendata['id']; if (empty($settings[$tid])) { continue; } $tokendata['assoc_user'] = $id; $tokendata['token'] = $settings[$tid]['token']; $tokendata['tokenkey'] = $settings[$tid]['tokenkey']; if ($inherit) { $tokendata['modules'] = $this->userman->getCombinedModuleSettingByID($id, 'restapi', 'restapi_modules'); $tokendata['users'] = $this->userman->getCombinedModuleSettingByID($id, 'restapi', 'restapi_users'); $tokendata['rate'] = $this->userman->getCombinedModuleSettingByID($id, 'restapi', 'restapi_rate'); $tokendata['token_status'] = $this->userman->getCombinedModuleSettingByID($id, 'restapi', 'restapi_token_status') ? 'enabled' : 'disabled'; } else { $tokendata['modules'] = !empty($settings[$tid]['modules']) ? $settings[$tid]['modules'] : array(); $tokendata['users'] = !empty($settings[$tid]['users']) ? $settings[$tid]['users'] : $defaultExt; $tokendata['rate'] = isset($settings[$tid]['rate']) ? $settings[$tid]['rate'] : '1000'; $tokendata['token_status'] = $settings[$tid]['token_status']; } $tokendata['name'] = 'User ' . $userdata['username'] . ' (autogen)'; $tokendata['desc'] = 'Autogenerated token on new user creation'; \restapi_tokens_put($tokendata); } //There are no pre-existing tokens so we create from whats been sent to us on the page } elseif (!empty($settings[0])) { $tokendata = \restapi_tokens_get(); $tid = 0; $tokendata['assoc_user'] = $id; $tokendata['token'] = $settings[$tid]['token']; $tokendata['tokenkey'] = $settings[$tid]['tokenkey']; $tokendata['modules'] = !empty($settings[$tid]['modules']) ? $settings[$tid]['modules'] : array(); $tokendata['users'] = !empty($settings[$tid]['users']) ? $settings[$tid]['users'] : $defaultExt; $tokendata['rate'] = $settings[$tid]['rate']; $tokendata['token_status'] = $settings[$tid]['token_status']; $tokendata['name'] = 'User ' . $userdata['username'] . ' (autogen)'; $tokendata['desc'] = 'Autogenerated token on new user creation'; \restapi_tokens_put($tokendata); //No tokens and no data so we need to generate a token now } else { $tokendata = \restapi_tokens_get(); $tokendata['assoc_user'] = $id; $tokendata['token'] = \restapi_tokens_generate(); $tokendata['tokenkey'] = \restapi_tokens_generate(); $tokendata['modules'] = array('*'); $tokendata['users'] = $defaultExt; $tokendata['token_status'] = 'disabled'; $tokendata['name'] = 'User ' . $userdata['username'] . ' (autogen)'; $tokendata['desc'] = 'Autogenerated token on new user creation'; \restapi_tokens_put($tokendata); } } }