get('/superuser/user', function () { // Require the user to be logged in global $response; $this->requirePermission('get_user'); // Get the user object $user = (new authentication())->get_user(); // Check if the request was successful if ($user) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER', 'Successfully fetched user'); $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); // Check if the user was found if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER', 'No user found'); // Return an error $response->error('User not found', 404); } // Return the list of users $response->success( $targetUser->includeIncludes(['all'])->asArray() ); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'GET_USER', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'get_user' => 'Get a user by ID' ] ); $this->get('/superuser/user/discounts', function () { // Require the user to be logged in global $response; $this->requirePermission('get_custom_prices_other'); // Get the user object $user = (new authentication())->get_user(); // Check if the request was successful if ($user) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_CUSTOM_PRICE', 'Successfully fetched custom price'); $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); // Check if the user was found if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_CUSTOM_PRICE', 'No user found'); // Return an error $response->error('User not found', 404); } // Return the list of users $response->success( $targetUser->getCustomPrices() ); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'GET_CUSTOM_PRICE', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'get_custom_prices_other' => 'Get custom prices for other users' ] ); $this->post('/superuser/user/discounts', function () { // Require the user to be logged in global /** @var response $response */ $response; $this->requirePermission('set_custom_price'); // Get the user object $user = (new authentication())->get_user(); // Check if the request was successful if ($user) { // Set the custom price $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_CUSTOM_PRICE', 'No user found'); // Return an error $response->add_meta('user_id', (int)$this->fromRequest('user_id')); $response->error('User not found (target)', 404); } // Check if the required fields are set $data = json_decode(file_get_contents('php://input'), true); if (!is_array($data)) { $response->error('Invalid request body', 400); } if (!isset($data['discount'])) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_CUSTOM_PRICE', 'No discount set'); // Return an error $response->error('No discount set', 400); } if (!isset($data['object_id'])) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_CUSTOM_PRICE', 'No object_id set'); // Return an error $response->error('No object_id set', 400); } if (!isset($data['is_category'])) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_CUSTOM_PRICE', 'No is_category set'); // Return an error $response->error('No is_category set', 400); } $discount = (int)$data['discount']; if ($discount < 0 || $discount > 100) { $response->error('Discount must be between 0 and 100', 400); } $is_category = (bool)$data['is_category']; if ($is_category) { $object_id = (string)$data['object_id']; } else { $object_id = (int)$data['object_id']; } $fixed_price_is_set = array_key_exists('fixed_price', $data); $fixed_price = null; if ($fixed_price_is_set) { if ($data['fixed_price'] === null || $data['fixed_price'] === '') { $fixed_price = null; } else { $fixed_price_value = filter_var($data['fixed_price'], FILTER_VALIDATE_INT); if ($fixed_price_value === false) { $response->error('Invalid fixed price', 400); } $fixed_price = (int)$fixed_price_value; } if ($fixed_price !== null && $fixed_price < 0) { $response->error('Fixed price must be zero or more', 400); } if ($is_category && $fixed_price !== null) { $response->error('Fixed price can only be set for products', 400); } } elseif (!$is_category) { $fixed_price = $targetUser->getProductFixedPrice((int)$object_id); } // Set the custom price $targetUser->setCustomPrice($targetUser->id, $object_id, $discount, $is_category, $fixed_price); try { (new economic_v2_versioning_service())->recordDiscountOverrideVersion( (int)$targetUser->id, (int)$targetUser->customer_number->value(), (bool)$is_category, (string)$object_id, (int)$discount, date('Y-m-d H:i:s'), 'live.discount_override.route', 1.0, false, [ 'route' => '/superuser/user/discounts', 'method' => 'POST', 'actor_user_id' => (int)$user->id, ], $fixed_price ); } catch (\Throwable $e) { (new logs_o())->add( 'users', 'global', 0, (int)$user->id, 'SET_CUSTOM_PRICE_VERSIONING_FAILED', $e->getMessage() ); } // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_CUSTOM_PRICE', 'Successfully set custom price'); // Return a success message $response->success('Successfully set custom price'); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'SET_CUSTOM_PRICE', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'set_custom_price' => 'Set custom price' ] ); $this->get('/admin/customer/getUserId', function () { // Require the user to be logged in global $response; // Get the user object $user = (new authentication())->get_user(); // Check if it's the users own id request if (!!$user && $this->isParametersSet(['customer_number']) && (int)$this->fromRequest('customer_number') === (int)$user->customer_number->value()) { $response->success( [ 'user_id' => $user->id ] ); } // Otherwise, require permission. $this->requirePermission('get_user_id'); // Check if the request was successful if ($user) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_ID', 'Successfully fetched user id'); $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); // Check if the user was found if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_ID', 'No user found'); // Return an error $response->error('User not found', 404); } // Return the list of users $response->success( [ 'user_id' => $targetUser->id ] ); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'GET_USER_ID', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'get_user_id' => 'Get user id from e-conomic customer number.' ] ); $this->get('/admin/customer/name', function () { // Require the user to be logged in global $response; $this->requirePermission('get_user_name'); // Get the user object $user = (new authentication())->get_user(); // Check if the request was successful if ($user) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_NAME', 'Successfully fetched user name'); $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); // Check if the user was found if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_NAME', 'No user found'); // Return an error $response->error('User not found', 404); } // Return the list of users $response->success( [ 'name' => (string)$targetUser->getCustomerName((int)$targetUser->customer_number->value()) ] ); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'GET_USER_NAME', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'get_user_name' => 'Get user full name from user ID.' ] ); $this->get('/superuser/user/keys', function () { // Require the user to be logged in global $response; $this->requirePermission('get_user_keys'); // Get the user object $user = (new authentication())->get_user(); // Check if the request was successful if ($user) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_KEYS', 'Successfully fetched user keys'); $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); // Check if the user was found if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_KEYS', 'No user found'); // Return an error $response->error('User not found', 404); } // Check if the request contains the key if (self::isParametersSet(['key'])) { // Check if the key is valid if (!$targetUser->keys->getValue(self::getParameter('key'))) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_KEYS', 'Invalid key'); // Return an error $response->error('Invalid key', 400); } // Return the key $response->success( [ self::getParameter('key') => $targetUser->keys->getValue(self::getParameter('key')) ] ); } // Return the list of users $response->success( $targetUser->keys->getAllKeys() ); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'GET_USER_KEYS', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'get_user_keys' => 'Get a user\'s keys, they are stored in the database as key-value pairs. THIS IS STRICTLY FOR ADMINISTRATION PURPOSES!' ] ); $this->post('/superuser/user/keys', function () { // Require the user to be logged in global $response; $this->requirePermission('set_user_keys'); // Get the user object $user = (new authentication())->get_user(); // Check if the request was successful if ($user) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_USER_KEYS', 'Successfully set user keys'); $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); // Check if the user was found if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_USER_KEYS', 'No user found'); // Return an error $response->error('User not found', 404); } // Check if the request contains the key if (!self::isParametersSet(['key'])) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_USER_KEYS', 'No key set'); // Return an error $response->error('No key set', 400); } // Check if the request contains the value if (!self::isParametersSet(['value'])) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_USER_KEYS', 'No value set'); // Return an error $response->error('No value set', 400); } // Set the key-value pair $targetUser->keys->setValue(self::getParameter('key'), self::getParameter('value')); // Return a success message $response->success('Successfully set user key'); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'SET_USER_KEYS', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'set_user_keys' => 'Set a user\'s keys, they are stored in the database as key-value pairs. THIS IS STRICTLY FOR ADMINISTRATION PURPOSES!' ] ); $this->post('/superuser/user/password', function () { // Require the user to be logged in global $response; $this->requirePermission('set_user_password'); // Get the user object $user = (new authentication())->get_user(); // Check if the request was successful if ($user) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_USER_PASSWORD', 'Successfully set user password'); $targetUser = (new users_o())->automaticGetTargetUserFromRequest(); // Check if the user was found if (!$targetUser->exists()) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_USER_PASSWORD', 'No user found'); // Return an error $response->error('User not found', 404); } // Check if the request contains the password if (!self::isParametersSet(['password'])) { // Log the incident (new logs_o())->add('users', 'global', 1, $user->id, 'SET_USER_PASSWORD', 'No password set'); // Return an error $response->error('No password set', 400); } // Set the password $targetUser->setPassword( self::getParameter('password') ); // Return a success message $response->success('Successfully set user password'); } else { // Log the incident (new logs_o())->add('users', 'global', 1, 0, 'SET_USER_PASSWORD', 'No user found, or invalid session'); // Return an error $response->error('Invalid session', 400); } }, [ 'set_user_password' => 'Set a user\'s password' ] ); } }