mirror of
https://github.com/humhub/humhub.git
synced 2025-02-25 11:43:30 +01:00
feat: canLike permissions added for owner, member, moderator, admin
This commit is contained in:
parent
6b41a60fa0
commit
631c976c39
@ -9,6 +9,9 @@
|
||||
namespace humhub\modules\like;
|
||||
|
||||
use Yii;
|
||||
use humhub\modules\like\models\Like;
|
||||
use humhub\modules\space\models\Space;
|
||||
use humhub\modules\content\components\ContentActiveRecord;
|
||||
|
||||
/**
|
||||
* This module provides like support for Content and Content Addons
|
||||
@ -36,6 +39,20 @@ class Module extends \humhub\components\Module
|
||||
*/
|
||||
public $isEnabled = true;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPermissions($contentContainer = null)
|
||||
{
|
||||
if ($contentContainer instanceof Space) {
|
||||
return [
|
||||
new permissions\CanLike()
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
@ -58,4 +75,28 @@ class Module extends \humhub\components\Module
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if given content object can be liked
|
||||
*
|
||||
* @param Like|ContentActiveRecord $object
|
||||
* @return boolean can like
|
||||
*/
|
||||
public function canLike($object)
|
||||
{
|
||||
$content = $object->content;
|
||||
|
||||
if ($content->container instanceof Space) {
|
||||
$space = $content->container;
|
||||
if (!$space->permissionManager->can(new permissions\CanLike())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($content->isArchived()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
59
protected/humhub/modules/like/permissions/CanLike.php
Normal file
59
protected/humhub/modules/like/permissions/CanLike.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @link https://www.humhub.org/
|
||||
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
|
||||
* @license https://www.humhub.com/licences
|
||||
*/
|
||||
|
||||
namespace humhub\modules\like\permissions;
|
||||
|
||||
use humhub\modules\space\models\Space;
|
||||
|
||||
/**
|
||||
* CreateComment Permission
|
||||
*/
|
||||
class CanLike extends \humhub\libs\BasePermission
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public $defaultAllowedGroups = [
|
||||
Space::USERGROUP_OWNER,
|
||||
Space::USERGROUP_ADMIN,
|
||||
Space::USERGROUP_MODERATOR,
|
||||
Space::USERGROUP_MEMBER,
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $fixedGroups = [
|
||||
// Space::USERGROUP_GUEST,
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $title = 'Add Like';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $description = 'Allows the user to add likes';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $moduleId = 'like';
|
||||
|
||||
public function __construct($config = [])
|
||||
{
|
||||
parent::__construct($config);
|
||||
|
||||
$this->title = \Yii::t('LikeModule.permissions', $this->title);
|
||||
$this->description = \Yii::t('LikeModule.permissions', $this->description);
|
||||
}
|
||||
|
||||
}
|
@ -3,10 +3,11 @@
|
||||
namespace humhub\modules\like\widgets;
|
||||
|
||||
use Yii;
|
||||
use humhub\modules\like\models\Like;
|
||||
use yii\helpers\Url;
|
||||
use yii\helpers\Html;
|
||||
|
||||
use humhub\modules\like\Module;
|
||||
use humhub\modules\like\models\Like as LikeModel;
|
||||
use humhub\modules\content\components\ContentActiveRecord;
|
||||
/**
|
||||
* This widget is used to show a like link inside the wall entry controls.
|
||||
*
|
||||
@ -19,7 +20,7 @@ class LikeLink extends \yii\base\Widget
|
||||
/**
|
||||
* The Object to be liked
|
||||
*
|
||||
* @var type
|
||||
* @var LikeModel|ContentActiveRecord
|
||||
*/
|
||||
public $object;
|
||||
|
||||
@ -33,8 +34,13 @@ class LikeLink extends \yii\base\Widget
|
||||
}
|
||||
|
||||
$currentUserLiked = false;
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('like');
|
||||
if (!$module->canLike($this->object)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$likes = Like::GetLikes($this->object->className(), $this->object->id);
|
||||
$likes = LikeModel::GetLikes(get_class($this->object), $this->object->id);
|
||||
foreach ($likes as $like) {
|
||||
if ($like->user->id == Yii::$app->user->id) {
|
||||
$currentUserLiked = true;
|
||||
@ -46,9 +52,9 @@ class LikeLink extends \yii\base\Widget
|
||||
'likes' => $likes,
|
||||
'currentUserLiked' => $currentUserLiked,
|
||||
'id' => $this->object->getUniqueId(),
|
||||
'likeUrl' => Url::to(['/like/like/like', 'contentModel' => $this->object->className(), 'contentId' => $this->object->id]),
|
||||
'unlikeUrl' => Url::to(['/like/like/unlike', 'contentModel' => $this->object->className(), 'contentId' => $this->object->id]),
|
||||
'userListUrl' => Url::to(['/like/like/user-list', 'contentModel' => $this->object->className(), 'contentId' => $this->object->getPrimaryKey()]),
|
||||
'likeUrl' => Url::to(['/like/like/like', 'contentModel' => get_class($this->object), 'contentId' => $this->object->id]),
|
||||
'unlikeUrl' => Url::to(['/like/like/unlike', 'contentModel' => get_class($this->object), 'contentId' => $this->object->id]),
|
||||
'userListUrl' => Url::to(['/like/like/user-list', 'contentModel' => get_class($this->object), 'contentId' => $this->object->getPrimaryKey()]),
|
||||
'title' => $this->generateLikeTitleText($currentUserLiked, $likes)
|
||||
]);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user