Local_group and User are now assumed to be in same namespace

This commit is contained in:
Mikael Nordfeldth 2013-10-17 01:16:03 +02:00
parent cd0e3f1fa4
commit 6ed66d9c76
15 changed files with 200 additions and 342 deletions

View File

@ -62,10 +62,4 @@ class ApiCheckNicknameAction extends ApiAction
$this->showJsonObjects($nickname_ok);
$this->endDocument('json');
}
function nicknameExists($nickname)
{
$user = User::staticGet('nickname', $nickname);
return ($user instanceof User);
}
}

View File

@ -71,7 +71,7 @@ class ApiGroupCreateAction extends ApiAuthAction
{
parent::prepare($args);
$this->nickname = Nickname::normalize($this->arg('nickname'));
$this->nickname = Nickname::normalize($this->arg('nickname'), true);
$this->fullname = $this->arg('full_name');
$this->homepage = $this->arg('homepage');
$this->description = $this->arg('description');
@ -130,22 +130,13 @@ class ApiGroupCreateAction extends ApiAuthAction
*/
function validateParams()
{
if ($this->groupNicknameExists($this->nickname)) {
// TRANS: Client error trying to create a group with a nickname this is already in use.
$this->clientError(_('Nickname already in use. Try another one.'), 403);
} elseif (!User_group::allowedNickname($this->nickname)) {
// TRANS: Client error in form for group creation.
$this->clientError(_('Not a valid nickname.'), 403);
} elseif (!is_null($this->homepage)
if (!is_null($this->homepage)
&& strlen($this->homepage) > 0
&& !common_valid_http_url($this->homepage)) {
// TRANS: Client error in form for group creation.
$this->clientError(_('Homepage is not a valid URL.'), 403);
} elseif (
!is_null($this->fullname)
} elseif (!is_null($this->fullname)
&& mb_strlen($this->fullname) > 255) {
// TRANS: Client error in form for group creation.
$this->clientError(_('Full name is too long (maximum 255 characters).'), 403);
@ -165,7 +156,7 @@ class ApiGroupCreateAction extends ApiAuthAction
if (!empty($this->aliasstring)) {
$this->aliases = array_map(
'common_canonical_nickname',
array('Nickname', 'normalize'), // static call to Nickname::normalize
array_unique(preg_split('/[\s,]+/', $this->aliasstring))
);
} else {
@ -183,53 +174,8 @@ class ApiGroupCreateAction extends ApiAuthAction
403);
}
foreach ($this->aliases as $alias) {
if (!Nickname::isValid($alias)) {
// TRANS: Client error shown when providing an invalid alias during group creation.
// TRANS: %s is the invalid alias.
$this->clientError(sprintf(_('Invalid alias: "%s".'), $alias), 403);
}
if ($this->groupNicknameExists($alias)) {
// TRANS: Client error displayed when trying to use an alias during group creation that is already in use.
// TRANS: %s is the alias that is already in use.
$this->clientError(sprintf(_('Alias "%s" already in use. Try another one.'), $alias), 403);
}
// XXX assumes alphanum nicknames
if (strcmp($alias, $this->nickname) == 0) {
// TRANS: Client error displayed when trying to use an alias during group creation that is the same as the group's nickname.
$this->clientError(_('Alias can\'t be the same as nickname.'), 403);
}
}
// Everything looks OK
return true;
}
/**
* Check to see whether a nickname is already in use by a group
*
* @param String $nickname The nickname in question
*
* @return boolean true or false
*/
function groupNicknameExists($nickname)
{
$local = Local_group::getKV('nickname', $nickname);
if (!empty($local)) {
return true;
}
$alias = Group_alias::getKV('alias', $nickname);
if (!empty($alias)) {
return true;
}
return false;
}
}

View File

@ -107,13 +107,18 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
try {
if (!empty($this->nickname)) {
if ($this->validateNickname()) {
$this->group->nickname = $this->nickname;
$this->group->mainpage = common_local_url(
'showgroup',
array('nickname' => $this->nickname)
);
try {
$this->group->nickname = Nickname::normalize($this->nickname, true);
} catch (NicknameTakenException $e) {
// Abort only if the nickname is occupied by _another_ local group
if ($e->profile->id != $this->group->id) {
throw new ApiValidationException($e->getMessage());
}
} catch (NicknameException $e) {
throw new ApiValidationException($e->getMessage());
}
$this->group->mainpage = common_local_url('showgroup',
array('nickname' => $this->group->nickname));
}
if (!empty($this->fullname)) {
@ -123,7 +128,7 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
if (!empty($this->homepage)) {
$this->validateHomepage();
$this->group->homepage = $this->hompage;
$this->group->homepage = $this->homepage;
}
if (!empty($this->description)) {
@ -137,7 +142,7 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
}
} catch (ApiValidationException $ave) {
$this->clientError($ave->getMessage(), 403);
$this->clientError($ave->getMessage(), 400);
}
$result = $this->group->update($orig);
@ -154,7 +159,6 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
if (!empty($this->aliasstring)) {
$aliases = $this->validateAliases();
}
} catch (ApiValidationException $ave) {
$this->clientError($ave->getMessage(), 403);
}
@ -187,54 +191,6 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
}
}
function nicknameExists($nickname)
{
$group = Local_group::getKV('nickname', $nickname);
if (!empty($group) &&
$group->group_id != $this->group->id) {
return true;
}
$alias = Group_alias::getKV('alias', $nickname);
if (!empty($alias) &&
$alias->group_id != $this->group->id) {
return true;
}
return false;
}
function validateNickname()
{
if (!Validate::string(
$this->nickname, array(
'min_length' => 1,
'max_length' => 64,
'format' => NICKNAME_FMT
)
)
) {
throw new ApiValidationException(
// TRANS: API validation exception thrown when nickname does not validate.
_('Nickname must have only lowercase letters and numbers and no spaces.')
);
} else if ($this->nicknameExists($this->nickname)) {
throw new ApiValidationException(
// TRANS: API validation exception thrown when nickname is already used.
_('Nickname already in use. Try another one.')
);
} else if (!User_group::allowedNickname($this->nickname)) {
throw new ApiValidationException(
// TRANS: API validation exception thrown when nickname does not validate.
_('Not a valid nickname.')
);
}
return true;
}
function validateHomepage()
{
if (!is_null($this->homepage)
@ -281,14 +237,12 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
function validateAliases()
{
$aliases = array_map(
'common_canonical_nickname',
array_unique(
preg_split('/[\s,]+/',
$this->aliasstring
)
)
);
try {
$aliases = array_map(array('Nickname', 'normalize'),
array_unique(preg_split('/[\s,]+/', $this->aliasstring)));
} catch (NicknameException $e) {
throw new ApiValidationException(sprintf('Error processing aliases: %s', $e->getMessage()));
}
if (count($aliases) > common_config('group', 'maxaliases')) {
// TRANS: API validation exception thrown when aliases do not validate.
@ -299,43 +253,6 @@ class ApiGroupProfileUpdateAction extends ApiAuthAction
common_config('group', 'maxaliases')));
}
foreach ($aliases as $alias) {
if (!Validate::string(
$alias, array(
'min_length' => 1,
'max_length' => 64,
'format' => NICKNAME_FMT)
)
) {
throw new ApiValidationException(
sprintf(
// TRANS: API validation exception thrown when aliases does not validate.
// TRANS: %s is the invalid alias.
_('Invalid alias: "%s".'),
$alias
)
);
}
if ($this->nicknameExists($alias)) {
throw new ApiValidationException(
sprintf(
// TRANS: API validation exception thrown when aliases is already used.
// TRANS: %s is the already used alias.
_('Alias "%s" already in use. Try another one.'),
$alias)
);
}
// XXX assumes alphanum nicknames
if (strcmp($alias, $this->nickname) == 0) {
throw new ApiValidationException(
// TRANS: API validation exception thrown when alias is the same as nickname.
_('Alias cannot be the same as nickname.')
);
}
}
return $aliases;
}
}

View File

@ -168,12 +168,25 @@ class EditgroupAction extends GroupAction
if (!$cur->isAdmin($this->group)) {
// TRANS: Client error displayed trying to edit a group while not being a group admin.
$this->clientError(_('You must be an admin to edit the group.'), 403);
return;
}
if (Event::handle('StartGroupSaveForm', array($this))) {
$nickname = Nickname::normalize($this->trimmed('newnickname'));
$nickname = $this->trimmed('newnickname');
try {
$nickname = Nickname::normalize($nickname, true);
} catch (NicknameTakenException $e) {
// Abort only if the nickname is occupied by _another_ group
if ($e->profile->id != $this->group->id) {
$this->showForm($e->getMessage());
return;
}
$nickname = Nickname::normalize($nickname); // without in-use check this time
} catch (NicknameException $e) {
$this->showForm($e->getMessage());
return;
}
$fullname = $this->trimmed('fullname');
$homepage = $this->trimmed('homepage');
$description = $this->trimmed('description');
@ -189,15 +202,7 @@ class EditgroupAction extends GroupAction
$join_policy = User_group::JOIN_POLICY_OPEN;
}
if ($this->nicknameExists($nickname)) {
// TRANS: Group edit form validation error.
$this->showForm(_('Nickname already in use. Try another one.'));
return;
} else if (!User_group::allowedNickname($nickname)) {
// TRANS: Group edit form validation error.
$this->showForm(_('Not a valid nickname.'));
return;
} else if (!is_null($homepage) && (strlen($homepage) > 0) &&
if (!is_null($homepage) && (strlen($homepage) > 0) &&
!common_valid_http_url($homepage)) {
// TRANS: Group edit form validation error.
$this->showForm(_('Homepage is not a valid URL.'));
@ -221,7 +226,8 @@ class EditgroupAction extends GroupAction
}
if (!empty($aliasstring)) {
$aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
$aliases = array_map(array('Nickname', 'normalize'),
array_unique(preg_split('/[\s,]+/', $aliasstring)));
} else {
$aliases = array();
}
@ -236,26 +242,6 @@ class EditgroupAction extends GroupAction
return;
}
foreach ($aliases as $alias) {
if (!Nickname::isValid($alias)) {
// TRANS: Group edit form validation error.
$this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
return;
}
if ($this->nicknameExists($alias)) {
// TRANS: Group edit form validation error.
$this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
$alias));
return;
}
// XXX assumes alphanum nicknames
if (strcmp($alias, $nickname) == 0) {
// TRANS: Group edit form validation error.
$this->showForm(_('Alias can\'t be the same as nickname.'));
return;
}
}
$this->group->query('BEGIN');
$orig = clone($this->group);
@ -271,7 +257,7 @@ class EditgroupAction extends GroupAction
$result = $this->group->update($orig);
if (!$result) {
if ($result === false) {
common_log_db_error($this->group, 'UPDATE', __FILE__);
// TRANS: Server error displayed when editing a group fails.
$this->serverError(_('Could not update group.'));
@ -304,23 +290,4 @@ class EditgroupAction extends GroupAction
$this->showForm(_('Options saved.'));
}
}
function nicknameExists($nickname)
{
$group = Local_group::getKV('nickname', $nickname);
if (!empty($group) &&
$group->group_id != $this->group->id) {
return true;
}
$alias = Group_alias::getKV('alias', $nickname);
if (!empty($alias) &&
$alias->group_id != $this->group->id) {
return true;
}
return false;
}
}

View File

@ -86,7 +86,7 @@ class NewgroupAction extends FormAction
parent::handlePost();
if (Event::handle('StartGroupSaveForm', array($this))) {
$nickname = Nickname::normalize($this->trimmed('newnickname'));
$nickname = Nickname::normalize($this->trimmed('newnickname'), true);
$fullname = $this->trimmed('fullname');
$homepage = $this->trimmed('homepage');
@ -95,13 +95,7 @@ class NewgroupAction extends FormAction
$private = $this->boolean('private');
$aliasstring = $this->trimmed('aliases');
if ($this->nicknameExists($nickname)) {
// TRANS: Group create form validation error.
throw new ClientException(_('Nickname already in use. Try another one.'));
} else if (!User_group::allowedNickname($nickname)) {
// TRANS: Group create form validation error.
throw new ClientException(_('Not a valid nickname.'));
} else if (!is_null($homepage) && (strlen($homepage) > 0) &&
if (!is_null($homepage) && (strlen($homepage) > 0) &&
!common_valid_http_url($homepage)) {
// TRANS: Group create form validation error.
throw new ClientException(_('Homepage is not a valid URL.'));
@ -121,7 +115,7 @@ class NewgroupAction extends FormAction
}
if (!empty($aliasstring)) {
$aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
$aliases = array_map(array('Nickname', 'normalize'), array_unique(preg_split('/[\s,]+/', $aliasstring)));
} else {
$aliases = array();
}
@ -136,24 +130,6 @@ class NewgroupAction extends FormAction
return;
}
foreach ($aliases as $alias) {
if (!Nickname::isValid($alias)) {
// TRANS: Group create form validation error.
// TRANS: %s is the invalid alias.
throw new ClientException(sprintf(_('Invalid alias: "%s"'), $alias));
}
if ($this->nicknameExists($alias)) {
// TRANS: Group create form validation error. %s is the already used alias.
throw new ClientException(sprintf(_('Alias "%s" already in use. Try another one.'),
$alias));
}
// XXX assumes alphanum nicknames
if (strcmp($alias, $nickname) == 0) {
// TRANS: Group create form validation error.
throw new ClientException(_('Alias cannot be the same as nickname.'));
}
}
if ($private) {
$force_scope = 1;
$join_policy = User_group::JOIN_POLICY_MODERATE;
@ -183,21 +159,4 @@ class NewgroupAction extends FormAction
common_redirect($group->homeUrl(), 303);
}
}
function nicknameExists($nickname)
{
$local = Local_group::getKV('nickname', $nickname);
if (!empty($local)) {
return true;
}
$alias = Group_alias::getKV('alias', $nickname);
if (!empty($alias)) {
return true;
}
return false;
}
}

View File

@ -243,8 +243,8 @@ class ProfilesettingsAction extends SettingsAction
try {
$nickname = Nickname::normalize($nickname, true);
} catch (NicknameTakenException $e) {
// Abort only if the nickname is occupied by another user
if ($e->byuser->id != $this->scoped->id) {
// Abort only if the nickname is occupied by another local profile
if ($e->profile->id != $this->scoped->id) {
$this->showForm($e->getMessage());
return;
}

View File

@ -53,4 +53,13 @@ class Group_alias extends Managed_DataObject
),
);
}
public function getProfile()
{
$group = User_group::getKV('id', $this->group_id);
if (!($group instanceof User_group)) {
return null; // TODO: Throw exception when other code is ready
}
return $group->getProfile();
}
}

View File

@ -38,6 +38,15 @@ class Local_group extends Managed_DataObject
);
}
public function getProfile()
{
$group = User_group::getKV('id', $this->group_id);
if (!($group instanceof User_group)) {
return null; // TODO: Throw exception when other code is ready
}
return $group->getProfile();
}
function setNickname($nickname)
{
$this->decache();

View File

@ -685,6 +685,11 @@ class User extends Managed_DataObject
function delete()
{
if (empty($this->id)) {
common_log(LOG_WARNING, "Ambiguous User->delete(); skipping related tables.");
return parent::delete();
}
try {
$profile = $this->getProfile();
$profile->delete();

View File

@ -73,6 +73,25 @@ class User_group extends Managed_DataObject
);
}
protected $_profile = null;
/**
* @return Profile
*
* @throws UserNoProfileException if user has no profile
*/
public function getProfile()
{
if (!($this->_profile instanceof Profile)) {
$this->_profile = Profile::getKV('id', $this->profile_id);
if (!($this->_profile instanceof Profile)) {
throw new GroupNoProfileException($this);
}
}
return $this->_profile;
}
public static function defaultLogo($size)
{
static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
@ -130,13 +149,6 @@ class User_group extends Managed_DataObject
return $stream->getNotices($offset, $limit, $since_id, $max_id);
}
function allowedNickname($nickname)
{
static $blacklist = array('new');
return !in_array($nickname, $blacklist);
}
function getMembers($offset=0, $limit=null) {
$ids = null;
if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) {
@ -392,7 +404,10 @@ class User_group extends Managed_DataObject
}
foreach ($to_insert as $insalias) {
$alias->alias = $insalias;
if ($insalias === $this->nickname) {
continue;
}
$alias->alias = Nickname::normalize($insalias, true);
$result = $alias->insert();
if (!$result) {
common_log_db_error($alias, 'INSERT', __FILE__);
@ -405,7 +420,7 @@ class User_group extends Managed_DataObject
static function getForNickname($nickname, $profile=null)
{
$nickname = common_canonical_nickname($nickname);
$nickname = Nickname::normalize($nickname);
// Are there any matching remote groups this profile's in?
if ($profile) {
@ -566,6 +581,8 @@ class User_group extends Managed_DataObject
}
}
$fields['nickname'] = Nickname::normalize($fields['nickname']);
// MAGICALLY put fields into current scope
// @fixme kill extract(); it makes debugging absurdly hard
@ -585,8 +602,6 @@ class User_group extends Managed_DataObject
$group = new User_group();
$group->query('BEGIN');
if (empty($uri)) {
// fill in later...
$uri = null;
@ -595,14 +610,33 @@ class User_group extends Managed_DataObject
$mainpage = common_local_url('showgroup', array('nickname' => $nickname));
}
$group->nickname = $nickname;
$group->fullname = $fullname;
$group->homepage = $homepage;
$group->description = $description;
$group->location = $location;
// We must create a new, incrementally assigned profile_id
$profile = new Profile();
$profile->nickname = $nickname;
$profile->fullname = $fullname;
$profile->profileurl = $mainpage;
$profile->homepage = $homepage;
$profile->bio = $description;
$profile->location = $location;
$profile->created = common_sql_now();
$group->nickname = $profile->nickname;
$group->fullname = $profile->fullname;
$group->homepage = $profile->homepage;
$group->description = $profile->bio;
$group->location = $profile->location;
$group->mainpage = $profile->profileurl;
$group->created = $profile->created;
$profile->query('BEGIN');
$id = $profile->insert();
if (empty($id)) {
$profile->query('ROLLBACK');
throw new ServerException(_('Profile insertion failed'));
}
$group->profile_id = $id;
$group->uri = $uri;
$group->mainpage = $mainpage;
$group->created = common_sql_now();
if (isset($fields['join_policy'])) {
$group->join_policy = intval($fields['join_policy']);
@ -677,11 +711,11 @@ class User_group extends Managed_DataObject
}
}
$group->query('COMMIT');
Event::handle('EndGroupSave', array($group));
}
$profile->query('COMMIT');
return $group;
}
@ -695,7 +729,17 @@ class User_group extends Managed_DataObject
*/
function delete()
{
if ($this->id) {
if (empty($this->id)) {
common_log(LOG_WARNING, "Ambiguous User_group->delete(); skipping related tables.");
return parent::delete();
}
try {
$profile = $this->getProfile();
$profile->delete();
} catch (GroupNoProfileException $unp) {
common_log(LOG_INFO, "Group {$this->nickname} has no profile; continuing deletion.");
}
// Safe to delete in bulk for now
@ -707,7 +751,6 @@ class User_group extends Managed_DataObject
Event::handle('UserGroupDeleteRelated', array($this, &$related));
foreach ($related as $cls) {
$inst = new $cls();
$inst->group_id = $this->id;
@ -729,17 +772,14 @@ class User_group extends Managed_DataObject
// to create a new group with one of those names or aliases.
$this->setAliases(array());
$local = Local_group::getKV('group_id', $this->id);
if ($local) {
if ($local instanceof Local_group) {
$local->delete();
}
// blow the cached ids
self::blow('user_group:notice_ids:%d', $this->id);
} else {
common_log(LOG_WARNING, "Ambiguous user_group->delete(); skipping related tables.");
}
parent::delete();
return parent::delete();
}
function isPrivate()

View File

@ -41,7 +41,7 @@ if (!defined('GNUSOCIAL')) { exit(1); }
class GroupNoProfileException extends NoProfileException
{
public $group = null;
protected $group = null;
/**
* constructor
@ -53,8 +53,8 @@ class GroupNoProfileException extends NoProfileException
{
$this->group = $group;
// TRANS: Exception text shown when no profile can be found for a user.
// TRANS: %1$s is a user nickname, $2$d is a user ID (number).
// TRANS: Exception text shown when no profile can be found for a group.
// TRANS: %1$s is a group nickname, $2$d is a group profile_id (number).
$message = sprintf(_('User %1$s (%2$d) has no profile record.'),
$group->nickname, $group->id);

View File

@ -126,9 +126,10 @@ class Nickname
throw new NicknameBlacklistedException();
} elseif (self::isSystemPath($str)) {
throw new NicknamePathCollisionException();
} elseif ($checkuse && $user = self::isTaken($str)) {
if ($user instanceof User) {
throw new NicknameTakenException($user);
} elseif ($checkuse) {
$profile = self::isTaken($str);
if ($profile instanceof Profile) {
throw new NicknameTakenException($profile);
}
}
@ -191,12 +192,26 @@ class Nickname
* Is the nickname already in use locally? Checks the User table.
*
* @param string $str
* @return User|null Returns null if no such user, otherwise a User object
* @return Profile|null Returns Profile if nickname found, otherwise null
*/
public static function isTaken($str)
{
$user = User::getKV('nickname', $str);
return $user; // null if no such User entry
$found = User::getKV('nickname', $str);
if ($found instanceof User) {
return $found->getProfile();
}
$found = Local_group::getKV('nickname', $str);
if ($found instanceof Local_group) {
return $found->getProfile();
}
$found = Group_alias::getKV('alias', $str);
if ($found instanceof Group_alias) {
return $found->getProfile();
}
return null;
}
}
@ -281,11 +296,11 @@ class NicknamePathCollisionException extends NicknameException
class NicknameTakenException extends NicknameException
{
public $user = null; // the User which occupies the nickname
public $profile = null; // the Profile which occupies the nickname
public function __construct(User $user, $msg=null, $code=400)
public function __construct(Profile $profile, $msg=null, $code=400)
{
$this->byuser = $user;
$this->profile = $profile;
if ($msg === null) {
$msg = $this->defaultMessage();

View File

@ -39,19 +39,18 @@ if (!defined('GNUSOCIAL')) { exit(1); }
* @link http://status.net/
*/
class NoProfileException extends NoProfileException
class NoProfileException extends ServerException
{
public $profile_id = null;
public function __construct($profile_id, $msg=null)
{
$this->id = (int)$profile_id;
$this->id = $profile_id;
if ($msg === null) {
// TRANS: Exception text shown when no profile can be found for a user.
// TRANS: %1$s is a user nickname, $2$d is a user ID (number).
$msg = sprintf(_('User %1$s (%2$d) has no profile record.'),
$group->nickname, $group->id);
$msg = sprintf(_('There is no profile with id==%u'), $this->id);
}
parent::__construct($msg, 404);

View File

@ -17,9 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Queue handler for letting plugins handle stuff.
@ -46,7 +44,7 @@ class PluginQueueHandler extends QueueHandler
{
try {
Event::handle('HandleQueuedNotice', array(&$notice));
} catch (UserNoProfileException $unp) {
} catch (NoProfileException $unp) {
// We can't do anything about this, so just skip
return true;
}

View File

@ -679,7 +679,7 @@ class TwitterauthorizationAction extends Action
try {
return Nickname::normalize($this->tw_fields['fullname'], true);
} catch (NicknameException $e) {
return null
return null;
}
}
}