N°3552 Add method to retrieve User object from Person object

This commit is contained in:
Stephen Abello
2021-07-29 16:36:30 +02:00
parent c9e887a264
commit f3c11e72cf

View File

@@ -1094,6 +1094,30 @@ class UserRights
}
}
/**
* @param Person $oPerson Person we try to match against Users contact (also Person objects)
* @param bool $bMustBeUnique If true, return null when 2+ Users matching this Person were found. Otherwise return the first one
*
* @return \DBObject|null
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MissingQueryArgument
* @throws \MySQLException
* @throws \MySQLHasGoneAwayException
* @since 3.0.0
*/
public static function GetUserFromPerson(Person $oPerson, bool $bMustBeUnique = true): ?DBObject
{
$sUserSearch = 'SELECT User WHERE contactid = :id';
$oUserSearch = DBObjectSearch::FromOQL($sUserSearch);
$oUserSearch->AllowAllData();
$oUserSet = new DBObjectSet($oUserSearch, array(), array('id' => $oPerson->GetKey()));
if($oUserSet->Count() > 0 && !($oUserSet->Count() > 1 && $bMustBeUnique)){
return $oUserSet->Fetch();
}
return null;
}
/**
* @return string
*/