diff --git a/protected/humhub/components/Controller.php b/protected/humhub/components/Controller.php index d9018e8000..5de7b7e557 100644 --- a/protected/humhub/components/Controller.php +++ b/protected/humhub/components/Controller.php @@ -24,7 +24,7 @@ class Controller extends \yii\web\Controller /** * @event \yii\base\Event an event raised on init a controller. */ - const EVENT_INIT = 'create'; + const EVENT_INIT = 'init'; /** * @var null|string the name of the sub layout to be applied to this controller's views. diff --git a/protected/humhub/docs/CHANGELOG.md b/protected/humhub/docs/CHANGELOG.md index 8b8480c1fa..a8f1ab068a 100644 --- a/protected/humhub/docs/CHANGELOG.md +++ b/protected/humhub/docs/CHANGELOG.md @@ -76,6 +76,7 @@ HumHub Change Log - Fix: Console image converter memory limit allocation - Enh: Added new controller init event - Enh: Made admin base controller method "getAccessRules()" non static +- Enh: Created new ImageController for user image and banner handling 1.2.0-beta.2 (February 24, 2017) -------------------------------- diff --git a/protected/humhub/models/forms/CropProfileImage.php b/protected/humhub/models/forms/CropProfileImage.php index 3dbeae08c0..3286c6b1e7 100644 --- a/protected/humhub/models/forms/CropProfileImage.php +++ b/protected/humhub/models/forms/CropProfileImage.php @@ -1,26 +1,13 @@ appendPageTitle(\Yii::t('UserModule.base', 'My Account')); return parent::init(); } - + /** * @inheritdoc */ @@ -46,7 +58,11 @@ class BaseAccountController extends \humhub\components\Controller */ public function getUser() { - return Yii::$app->user->getIdentity(); + if ($this->user === null) { + $this->user = Yii::$app->user->getIdentity(); + } + + return $this->user; } } diff --git a/protected/humhub/modules/user/controllers/AccountController.php b/protected/humhub/modules/user/controllers/AccountController.php index d3b08db204..9b91d2347b 100644 --- a/protected/humhub/modules/user/controllers/AccountController.php +++ b/protected/humhub/modules/user/controllers/AccountController.php @@ -13,6 +13,8 @@ use yii\web\HttpException; use humhub\modules\user\components\BaseAccountController; use humhub\modules\user\models\User; use humhub\modules\notification\models\forms\NotificationSettings; +use humhub\modules\user\controllers\ImageController; + /** * AccountController provides all standard actions for the current logged in * user account. @@ -23,6 +25,9 @@ use humhub\modules\notification\models\forms\NotificationSettings; class AccountController extends BaseAccountController { + /** + * @inheritdoc + */ public function init() { $this->setActionTitles([ @@ -213,7 +218,7 @@ class AccountController extends BaseAccountController if (!$user->isModuleEnabled($moduleId)) { $user->enableModule($moduleId); } - + if (!Yii::$app->request->isAjax) { return $this->redirect(['/user/account/edit-modules']); } else { @@ -233,7 +238,7 @@ class AccountController extends BaseAccountController $user->disableModule($moduleId); } - if (!Yii::$app->request->isAjax) { + if (!Yii::$app->request->isAjax) { return $this->redirect(['/user/account/edit-modules']); } else { Yii::$app->response->format = 'json'; @@ -366,129 +371,57 @@ class AccountController extends BaseAccountController /** * Crops the banner image of the user + * @deprecated since version 1.2 */ - public function actionCropBannerImage() + public function actionCropBannerImageXX() { - $model = new \humhub\models\forms\CropProfileImage(); - $profileImage = new \humhub\libs\ProfileBannerImage($this->getUser()->guid); - - if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $profileImage->cropOriginal($model->cropX, $model->cropY, $model->cropH, $model->cropW); - return $this->htmlRedirect($this->getUser()->getUrl()); - } - - return $this->renderAjax('cropBannerImage', ['model' => $model, 'profileImage' => $profileImage, 'user' => $this->getUser()]); + return Yii::$app->runAction('/user/image/crop', ['type' => ImageController::TYPE_PROFILE_BANNER_IMAGE]); } /** * Handle the banner image upload + * + * @deprecated since version 1.2 */ - public function actionBannerImageUpload() + public function actionBannerImageUploadXX() { - \Yii::$app->response->format = 'json'; - - $model = new \humhub\models\forms\UploadProfileImage(); - $json = array(); - - $files = \yii\web\UploadedFile::getInstancesByName('bannerfiles'); - $file = $files[0]; - $model->image = $file; - - if ($model->validate()) { - $profileImage = new \humhub\libs\ProfileBannerImage($this->getUser()->guid); - $profileImage->setNew($model->image); - - $json['error'] = false; - $json['name'] = ""; - $json['url'] = $profileImage->getUrl(); - $json['size'] = $model->image->size; - $json['deleteUrl'] = ""; - $json['deleteType'] = ""; - } else { - $json['error'] = true; - $json['errors'] = $model->getErrors(); + // Ensure view file backward compatibility prior 1.2 + if (isset($_FILES['bannerfiles'])) { + $_FILES['images'] = $_FILES['bannerfiles']; } - - return ['files' => $json]; + return Yii::$app->runAction('/user/image/upload', ['type' => ImageController::TYPE_PROFILE_BANNER_IMAGE]); } /** * Handle the profile image upload + * + * @deprecated since version 1.2 */ - public function actionProfileImageUpload() + public function actionProfileImageUploadXX() { - \Yii::$app->response->format = 'json'; - - $model = new \humhub\models\forms\UploadProfileImage(); - - $json = array(); - - $files = \yii\web\UploadedFile::getInstancesByName('profilefiles'); - $file = $files[0]; - $model->image = $file; - - if ($model->validate()) { - - $json['error'] = false; - - $profileImage = new \humhub\libs\ProfileImage($this->getUser()->guid); - $profileImage->setNew($model->image); - - $json['name'] = ""; - $json['url'] = $profileImage->getUrl(); - $json['size'] = $model->image->size; - $json['deleteUrl'] = ""; - $json['deleteType'] = ""; - } else { - $json['error'] = true; - $json['errors'] = $model->getErrors(); + // Ensure view file backward compatibility prior 1.2 + if (isset($_FILES['profilefiles'])) { + $_FILES['images'] = $_FILES['profilefiles']; } - - return array('files' => $json); + return Yii::$app->runAction('/user/image/upload', ['type' => ImageController::TYPE_PROFILE_IMAGE]); } /** * Crops the profile image of the user + * @deprecated since version 1.2 */ - public function actionCropProfileImage() + public function actionCropProfileImageXX() { - $model = new \humhub\models\forms\CropProfileImage(); - $profileImage = new \humhub\libs\ProfileImage($this->getUser()->guid); - - if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $profileImage->cropOriginal($model->cropX, $model->cropY, $model->cropH, $model->cropW); - return $this->htmlRedirect($this->getUser()->getUrl()); - } - - return $this->renderAjax('cropProfileImage', array('model' => $model, 'profileImage' => $profileImage, 'user' => $this->getUser())); + return Yii::$app->runAction('/user/image/crop', ['type' => ImageController::TYPE_PROFILE_IMAGE]); } /** * Deletes the profile image or profile banner + * @deprecated since version 1.2 */ - public function actionDeleteProfileImage() + public function actionDeleteProfileImageXX() { - \Yii::$app->response->format = 'json'; - - $this->forcePostRequest(); - - $type = Yii::$app->request->get('type', 'profile'); - - $json = array('type' => $type); - - $image = null; - if ($type == 'profile') { - $image = new \humhub\libs\ProfileImage($this->getUser()->guid); - } elseif ($type == 'banner') { - $image = new \humhub\libs\ProfileBannerImage($this->getUser()->guid); - } - - if ($image) { - $image->delete(); - $json['defaultUrl'] = $image->getUrl(); - } - - return $json; + return Yii::$app->runAction('/user/image/delete', ['type' => (Yii::$app->request->get('type', 'profile') == 'profile') ? ImageController::TYPE_PROFILE_IMAGE : ImageController::TYPE_PROFILE_BANNER_IMAGE]); } /** diff --git a/protected/humhub/modules/user/controllers/ImageController.php b/protected/humhub/modules/user/controllers/ImageController.php new file mode 100644 index 0000000000..36e30d9917 --- /dev/null +++ b/protected/humhub/modules/user/controllers/ImageController.php @@ -0,0 +1,198 @@ +user->isGuest) { + if (Yii::$app->user->getIdentity()->isSystemAdmin() && Yii::$app->getModule('user')->adminCanChangeUserProfileImages) { + $this->allowModifyProfileBanner = true; + $this->allowModifyProfileImage = true; + } elseif (Yii::$app->user->getIdentity()->id == $this->getUser()->id) { + $this->allowModifyProfileBanner = true; + $this->allowModifyProfileImage = true; + } + } + + // Make sure to execute this on the end of initialization, to allow events + // to modify the attributes (e.g. allowModifyProfileImage) + parent::init(); + } + + /** + * Uploads a new image + * + * @param string $type + * @return \yii\web\Response the response + */ + public function actionUpload($type) + { + $model = new UploadProfileImage(); + + $files = UploadedFile::getInstancesByName('images'); + if (isset($files[0])) { + $model->image = $files[0]; + } + + if (!$model->validate()) { + return $this->asJson(['files' => [ + 'error' => true, + 'errors' => $model->getErrors() + ]]); + } + + $image = $this->getProfileImage($type); + $image->setNew($model->image); + + return $this->asJson(['files' => [ + 'name' => '', + 'deleteUrl' => '', + 'deleteType' => '', + 'size' => $model->image->size, + 'url' => $image->getUrl(), + ]]); + } + + /** + * Crops a image + * + * @param string $type + * @return \yii\web\Response the response + */ + public function actionCrop($type) + { + $model = new CropProfileImage(); + + if ($type == static::TYPE_PROFILE_IMAGE) { + $title = Yii::t('UserModule.account', 'Modify your profile image'); + } elseif ($type == static::TYPE_PROFILE_BANNER_IMAGE) { + $title = Yii::t('UserModule.account', 'Modify your title image'); + $model->aspectRatio = '6.3'; + $model->cropSetSelect = [0, 0, 267, 48]; + } + + $image = $this->getProfileImage($type); + if ($model->load(Yii::$app->request->post()) && $model->validate()) { + $image->cropOriginal($model->cropX, $model->cropY, $model->cropH, $model->cropW); + return $this->htmlRedirect($this->getUser()->getUrl()); + } + + return $this->renderAjax('crop', [ + 'model' => $model, + 'profileImage' => $image, + 'user' => $this->getUser(), + 'type' => $type, + 'title' => $title, + ]); + } + + /** + * Delete an image + * + * @param string $type + * @return \yii\web\Response the response + */ + public function actionDelete($type) + { + Yii::$app->response->format = 'json'; + + $this->forcePostRequest(); + + $image = $this->getProfileImage($type); + $image->delete(); + + return $this->asJson([ + 'type' => $type, + 'defaultUrl' => $image->getUrl() + ]); + } + + /** + * Returns the Profile Image + * + * @param string $type + * @return ProfileImage|ProfileBannerImage + * @throws HttpException + */ + protected function getProfileImage($type) + { + if ($type == static::TYPE_PROFILE_IMAGE) { + if (!$this->allowModifyProfileImage) { + throw new HttpException(403, 'Access denied!'); + } + return new ProfileImage($this->getUser()->guid); + } elseif ($type == static::TYPE_PROFILE_BANNER_IMAGE) { + if (!$this->allowModifyProfileBanner) { + throw new HttpException(403, 'Access denied!'); + } + return new ProfileBannerImage($this->getUser()->guid); + } else { + throw new HttpException(400, 'Invalid image type given!'); + } + } + + /** + * Returns the current user of this account + * + * An administration can also pass a user id via GET parameter to change users + * accounts settings. + * + * @return User the user + */ + public function getUser() + { + if ($this->user === null && Yii::$app->request->get('userGuid') != '' && Yii::$app->user->getIdentity()->isSystemAdmin()) { + $user = User::findOne(['guid' => Yii::$app->request->get('userGuid')]); + if ($user === null) { + throw new HttpException(404, 'Could not find user!'); + } + $this->user = $user; + } + + return parent::getUser(); + } + +} diff --git a/protected/humhub/modules/user/views/account/cropProfileImage.php b/protected/humhub/modules/user/views/account/cropProfileImage.php deleted file mode 100644 index 5c3f62e7fb..0000000000 --- a/protected/humhub/modules/user/views/account/cropProfileImage.php +++ /dev/null @@ -1,88 +0,0 @@ - - - - - - - - diff --git a/protected/humhub/modules/user/views/account/cropBannerImage.php b/protected/humhub/modules/user/views/image/crop.php similarity index 86% rename from protected/humhub/modules/user/views/account/cropBannerImage.php rename to protected/humhub/modules/user/views/image/crop.php index 8b9512698b..813c1b0afc 100644 --- a/protected/humhub/modules/user/views/account/cropBannerImage.php +++ b/protected/humhub/modules/user/views/image/crop.php @@ -14,19 +14,14 @@ use yii\helpers\Url;