mirror of
https://github.com/typemill/typemill.git
synced 2025-08-09 15:47:09 +02:00
Search users, select role field, german translations
This commit is contained in:
@@ -6,6 +6,14 @@ use Typemill\Models\Field;
|
||||
|
||||
class Fields
|
||||
{
|
||||
|
||||
protected $c;
|
||||
|
||||
public function __construct($c = NULL)
|
||||
{
|
||||
$this->c = $c;
|
||||
}
|
||||
|
||||
public function getFields($userSettings, $objectType, $objectName, $objectSettings, $formType = false)
|
||||
{
|
||||
# hold all fields in array
|
||||
@@ -45,6 +53,17 @@ class Fields
|
||||
$fieldConfigurations['description'] = $userSettings[$objectType][$objectName][$fieldConfigurations['description']];
|
||||
}
|
||||
|
||||
# check if the field is a select field with dataset = userroles
|
||||
if(isset($this->c) && isset($fieldConfigurations['type']) && ($fieldConfigurations['type'] == 'select' ) && isset($fieldConfigurations['dataset']) && ($fieldConfigurations['dataset'] == 'userroles' ) )
|
||||
{
|
||||
$userroles = [null => null];
|
||||
foreach($this->c->acl->getRoles() as $userrole)
|
||||
{
|
||||
$userroles[$userrole] = $userrole;
|
||||
}
|
||||
$fieldConfigurations['options'] = $userroles;
|
||||
}
|
||||
|
||||
# for each field generate a new field object with the field name and the field configurations
|
||||
$field = new Field($fieldName, $fieldConfigurations);
|
||||
|
||||
|
@@ -4,6 +4,9 @@ namespace Typemill\Models;
|
||||
|
||||
class User extends WriteYaml
|
||||
{
|
||||
|
||||
private $userDir = __DIR__ . '/../../settings/users';
|
||||
|
||||
public function getUsers()
|
||||
{
|
||||
$userDir = __DIR__ . '/../../settings/users';
|
||||
@@ -12,96 +15,15 @@ class User extends WriteYaml
|
||||
if(!is_dir($userDir)){ return array(); }
|
||||
|
||||
/* get all user files */
|
||||
$users = array_diff(scandir($userDir), array('..', '.'));
|
||||
$userfiles = array_diff(scandir($userDir), array('..', '.', '.logins', 'tmuserindex-mail.txt', 'tmuserindex-role.txt'));
|
||||
|
||||
$cleanUser = array();
|
||||
foreach($users as $key => $user)
|
||||
$usernames = array();
|
||||
foreach($userfiles as $key => $userfile)
|
||||
{
|
||||
if($user == '.logins'){ continue; }
|
||||
$cleanUser[] = str_replace('.yaml', '', $user);
|
||||
$usernames[] = str_replace('.yaml', '', $userfile);
|
||||
}
|
||||
|
||||
return $cleanUser;
|
||||
}
|
||||
|
||||
# returns array of emails of all users
|
||||
public function getUserMails()
|
||||
{
|
||||
$userDir = __DIR__ . '/../../settings/users';
|
||||
|
||||
/* check if users directory exists */
|
||||
if(!is_dir($userDir)){ return array(); }
|
||||
|
||||
/* get all user files */
|
||||
$users = array_diff(scandir($userDir), array('..', '.'));
|
||||
|
||||
$usermails = array();
|
||||
|
||||
foreach($users as $key => $user)
|
||||
{
|
||||
if($user == '.logins'){ continue; }
|
||||
|
||||
$contents = file_get_contents($userDir . DIRECTORY_SEPARATOR . $user);
|
||||
|
||||
if($contents === false){ continue; }
|
||||
|
||||
$searchfor = 'email:';
|
||||
|
||||
# escape special characters in the query
|
||||
$pattern = preg_quote($searchfor, '/');
|
||||
|
||||
# finalise the regular expression, matching the whole line
|
||||
$pattern = "/^.*$pattern.*\$/m";
|
||||
|
||||
# search, and store first occurence in $matches
|
||||
if(preg_match($pattern, $contents, $match)){
|
||||
$usermails[] = trim(str_replace("email:", "", $match[0]));
|
||||
}
|
||||
}
|
||||
return $usermails;
|
||||
}
|
||||
|
||||
public function findUserByEmail($email)
|
||||
{
|
||||
$userDir = __DIR__ . '/../../settings/users';
|
||||
|
||||
/* check if users directory exists */
|
||||
if(!is_dir($userDir)){ return array(); }
|
||||
|
||||
/* get all user files */
|
||||
$users = array_diff(scandir($userDir), array('..', '.'));
|
||||
|
||||
$usermails = array();
|
||||
|
||||
foreach($users as $key => $user)
|
||||
{
|
||||
if($user == '.logins'){ continue; }
|
||||
|
||||
$contents = file_get_contents($userDir . DIRECTORY_SEPARATOR . $user);
|
||||
|
||||
if($contents === false){ continue; }
|
||||
|
||||
$searchfor = 'email:';
|
||||
|
||||
# escape special characters in the query
|
||||
$pattern = preg_quote($searchfor, '/');
|
||||
|
||||
# finalise the regular expression, matching the whole line
|
||||
$pattern = "/^.*$pattern.*\$/m";
|
||||
|
||||
# search, and store first occurence in $matches
|
||||
if(preg_match($pattern, $contents, $match)){
|
||||
$usermail = trim(str_replace("email:", "", $match[0]));
|
||||
|
||||
if($usermail == $email)
|
||||
{
|
||||
$user = \Symfony\Component\Yaml\Yaml::parse($contents);
|
||||
unset($user['password']);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return $usernames;
|
||||
}
|
||||
|
||||
public function getUser($username)
|
||||
@@ -123,6 +45,8 @@ class User extends WriteYaml
|
||||
|
||||
if($this->updateYaml('settings/users', $params['username'] . '.yaml', $params))
|
||||
{
|
||||
$this->deleteUserIndex();
|
||||
|
||||
return $params['username'];
|
||||
}
|
||||
return false;
|
||||
@@ -153,6 +77,8 @@ class User extends WriteYaml
|
||||
|
||||
$this->updateYaml('settings/users', $userdata['username'] . '.yaml', $update);
|
||||
|
||||
$this->deleteUserIndex();
|
||||
|
||||
# if user updated his own profile, update session data
|
||||
if(isset($_SESSION['user']) && $_SESSION['user'] == $params['username'])
|
||||
{
|
||||
@@ -176,16 +102,11 @@ class User extends WriteYaml
|
||||
if($this->getUser($username))
|
||||
{
|
||||
unlink('settings/users/' . $username . '.yaml');
|
||||
|
||||
$this->deleteUserIndex();
|
||||
}
|
||||
}
|
||||
|
||||
/* replaced by ACL
|
||||
public function getUserroles()
|
||||
{
|
||||
return array('administrator', 'editor');
|
||||
}
|
||||
*/
|
||||
|
||||
public function login($username)
|
||||
{
|
||||
$user = $this->getUser($username);
|
||||
@@ -216,5 +137,187 @@ class User extends WriteYaml
|
||||
public function generatePassword($password)
|
||||
{
|
||||
return \password_hash($password, PASSWORD_DEFAULT, ['cost' => 10]);
|
||||
}
|
||||
|
||||
|
||||
# accepts email with or without asterix and returns userdata
|
||||
public function findUsersByEmail($email)
|
||||
{
|
||||
# get all user files
|
||||
$usernames = $this->getUsers();
|
||||
|
||||
$countusers = count($usernames);
|
||||
|
||||
if($countusers == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
# use a simple dirty search if there are less than 10 users (only in use for new user registrations)
|
||||
if($countusers <= 4)
|
||||
{
|
||||
foreach($usernames as $key => $username)
|
||||
{
|
||||
$userdata = $this->getSecureUser($username);
|
||||
|
||||
if($userdata['email'] == $email)
|
||||
{
|
||||
return $userdata;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# if there are more than 10 users, search with an index
|
||||
$usermails = $this->getUserMailIndex($usernames);
|
||||
|
||||
# search with starting asterix, ending asterix or without asterix
|
||||
if($email[0] == '*')
|
||||
{
|
||||
$userdata = [];
|
||||
$search = substr($email, 1);
|
||||
$length = strlen($search);
|
||||
|
||||
foreach($usermails as $usermail => $username)
|
||||
{
|
||||
if(substr($usermail, -$length) == $search)
|
||||
{
|
||||
$userdata[] = $username;
|
||||
}
|
||||
}
|
||||
|
||||
$userdata = empty($userdata) ? false : $userdata;
|
||||
|
||||
return $userdata;
|
||||
}
|
||||
elseif(substr($email, -1) == '*')
|
||||
{
|
||||
$userdata = [];
|
||||
$search = substr($email, 0, -1);
|
||||
$length = strlen($search);
|
||||
|
||||
foreach($usermails as $usermail => $username)
|
||||
{
|
||||
if(substr($usermail, 0, $length) == $search)
|
||||
{
|
||||
$userdata[] = $username;
|
||||
}
|
||||
}
|
||||
|
||||
$userdata = empty($userdata) ? false : $userdata;
|
||||
|
||||
return $userdata;
|
||||
}
|
||||
elseif(isset($usermails[$email]))
|
||||
{
|
||||
$userdata[] = $usermails[$email];
|
||||
return $userdata;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getUserMailIndex($usernames)
|
||||
{
|
||||
$userDir = __DIR__ . '/../../settings/users';
|
||||
|
||||
if(file_exists($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-mail.txt'))
|
||||
{
|
||||
# read and return the file
|
||||
$usermailindex = file($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-mail.txt');
|
||||
}
|
||||
|
||||
$usermailindex = array();
|
||||
|
||||
foreach($usernames as $key => $username)
|
||||
{
|
||||
$userdata = $this->getSecureUser($username);
|
||||
|
||||
$usermailindex[$userdata['email']] = $username;
|
||||
}
|
||||
|
||||
file_put_contents($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-mail.txt', var_export($usermailindex, TRUE));
|
||||
|
||||
return $usermailindex;
|
||||
}
|
||||
|
||||
# accepts email with or without asterix and returns usernames
|
||||
public function findUsersByRole($role)
|
||||
{
|
||||
# get all user files
|
||||
$usernames = $this->getUsers();
|
||||
|
||||
/*
|
||||
$countusers = count($usernames);
|
||||
|
||||
if($countusers == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
# use a simple dirty search if there are less than 10 users (not in use right now)
|
||||
if($countusers <= 4)
|
||||
{
|
||||
$userdata = [];
|
||||
foreach($usernames as $key => $username)
|
||||
{
|
||||
$userdetails = $this->getSecureUser($username);
|
||||
|
||||
if($userdetails['userrole'] == $role)
|
||||
{
|
||||
$userdata[] = $userdetails;
|
||||
}
|
||||
}
|
||||
if(empty($userdata))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $userdata;
|
||||
}
|
||||
*/
|
||||
$userroles = $this->getUserRoleIndex($usernames);
|
||||
|
||||
if(isset($userroles[$role]))
|
||||
{
|
||||
return $userroles[$role];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getUserRoleIndex($usernames)
|
||||
{
|
||||
$userDir = __DIR__ . '/../../settings/users';
|
||||
|
||||
if(file_exists($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-role.txt'))
|
||||
{
|
||||
# read and return the file
|
||||
$userroleindex = file($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-role.txt');
|
||||
}
|
||||
|
||||
$userroleindex = array();
|
||||
|
||||
foreach($usernames as $key => $username)
|
||||
{
|
||||
$userdata = $this->getSecureUser($username);
|
||||
|
||||
$userroleindex[$userdata['userrole']][] = $username;
|
||||
}
|
||||
|
||||
file_put_contents($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-role.txt', var_export($userroleindex, TRUE));
|
||||
|
||||
return $userroleindex;
|
||||
}
|
||||
|
||||
protected function deleteUserIndex()
|
||||
{
|
||||
$userDir = __DIR__ . '/../../settings/users';
|
||||
|
||||
if(file_exists($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-mail.txt'))
|
||||
{
|
||||
# read and return the file
|
||||
unlink($userDir . DIRECTORY_SEPARATOR . 'tmuserindex-mail.txt');
|
||||
}
|
||||
}
|
||||
}
|
@@ -39,8 +39,8 @@ class Validation
|
||||
# checks if email is available if user is created
|
||||
Validator::addRule('emailAvailable', function($field, $value, array $params, array $fields) use ($user)
|
||||
{
|
||||
$usermails = $user->getUserMails();
|
||||
if(in_array(trim($value), $usermails)){ return false; }
|
||||
$email = trim($value);
|
||||
if($user->findUsersByEmail($email)){ return false; }
|
||||
return true;
|
||||
}, 'taken');
|
||||
|
||||
@@ -50,8 +50,8 @@ class Validation
|
||||
$userdata = $user->getSecureUser($fields['username']);
|
||||
if($userdata['email'] == $value){ return true; } # user has not updated his email
|
||||
|
||||
$usermails = $user->getUserMails();
|
||||
if(in_array(trim($value), $usermails)){ return false; }
|
||||
$email = trim($value);
|
||||
if($user->findUsersByEmail($email)){ return false; }
|
||||
return true;
|
||||
}, 'taken');
|
||||
|
||||
|
Reference in New Issue
Block a user