xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / user.php (1f9f35c78a1125d4a9f4124efb9e76ae4924d38c) (2,877B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = "../inc";
require_once($INC . "/user.inc.php");
require_once($INC . "/db/struct.inc.php");

rg_log_set_file("user.log");

$rg_user_max_len = 20;
$rg_pass_key = "aaa";
$rg_session_time = 3600;
$rg_user_allow = '/^[A-Za-z0-9_.-]*$/';


$db = rg_sql_open("dbname=trg");
if ($db === FALSE) {
	echo "Cannot create a database (" . rg_sql_error() . ")!\n";
	exit(1);
}

$ignore_errors = FALSE;
$drop_tables = TRUE;
$old_schema_ver = 0;
$r = rg_sql_struct_run($db, $ignore_errors, $drop_tables, $old_schema_ver);
if ($r === FALSE) {
	echo "Cannot create structure!\n";
	exit(1);
}

// add user
$r = rg_user_edit($db, 0, "userA", "rg@localhost", "pass1", 1, 100, "C", 3600, "");
if ($r !== TRUE) {
	echo "Cannot add user (" . rg_user_error() . ")!\n";
	exit(1);
}
$uid = rg_sql_last_id($db);

$v = rg_user_forgot_pass_mail($db, "rg@localhost");
if ($v === FALSE) {
	echo "Error: " . user_error() . "!\n";
	exit(1);
}

$_ui = rg_user_info($db, $uid, "", "");
if ($_ui['exists'] != 1) {
	echo "Seems that user with $uid does not exists!\n";
	exit(1);
}
$salt = $_ui['salt'];
$pass = $_ui['pass'];

// edit user - empty pass
$r = rg_user_edit($db, $uid, "userA", "rg@localhost", "", 1, 100, "C", 3600, "");
if ($r !== TRUE) {
	echo "Cannot edit user with empty pass (" . rg_user_error() . ")!\n";
	exit(1);
}
// the pass should not be changed here
$_ui = rg_user_info($db, $uid, "", "");
if ($_ui['exists'] != 1) {
	echo "Seems that user with $uid does not exists!\n";
	exit(1);
}
if (strcmp($salt, $_ui['salt']) != 0) {
	echo "Salt was changed!\n";
	exit(1);
}
if (strcmp($pass, $_ui['pass']) != 0) {
	echo "Password was changed!\n";
	exit(1);
}

// edit user - no empty pass
$r = rg_user_edit($db, $uid, "userA", "rg@localhost", "pass2", 1, 100, "C", 3600, "");
if ($r !== TRUE) {
	echo "Cannot edit user with not empty pass (" . rg_user_error() . ")!\n";
	exit(1);
}
// the pass should be changed here
$_ui = rg_user_info($db, $uid, "", "");
if ($_ui['exists'] != 1) {
	echo "Seems that user with $uid does not exists!\n";
	exit(1);
}
if (strcmp($salt, $_ui['salt']) == 0) {
	echo "Salt was not changed!\n";
	exit(1);
}
if (strcmp($pass, $_ui['pass']) == 0) {
	echo "Password was not changed!\n";
	exit(1);
}

// change password
$r = rg_user_set_pass($db, $uid, "pass3");
if ($r !== TRUE) {
	echo "Cannot change pass!\n";
	exit(1);
}

// get token for e-mail forgot pass feature
$token = rg_user_forgot_pass_mail_prepare($db, "rg@localhost");
if ($token === FALSE) {
	echo "Could not get token (" . rg_user_error() . ")!\n";
	exit(1);
}

$r = rg_user_forgot_pass_uid($db, $token);
if ($r['ok'] != 1) {
	echo "Cannot find uid based on token (" . rg_user_error() . ")!\n";
	exit(1);
}

if ($r['uid'] != $uid) {
	echo "Token returned does not belong to the proper user!\n";
	exit(1);
}

echo "user: OK\n";
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master