Skip to content

Commit 29a878f

Browse files
authored
Merge pull request #545 from TonisOrmisson/limit-profile-view
Add option to limit profile views only for admin users
2 parents 65a35e2 + 25c7b90 commit 29a878f

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fix: Social Network Auth (eluhr)
88
- Enh #532: /user/registration/register now shows form validation errors
99
- Enh: Allow/suggest new v3 releases of 2amigos 2fa dependencies: 2fa-library, qrcode-library (TonisOrmisson)
10+
- Enh: Added option to disable viewing any other user's profile for non-admin users (TonisOrmisson)
1011

1112
## 1.6.2 Jan 4th, 2024
1213

docs/install/configuration-options.md

+5
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ Set to `true` to restrict user assignments to roles only.
313313

314314
If `true` registration and last login IPs are not logged into users table, instead a dummy 127.0.0.1 is used
315315

316+
317+
#### disableProfileViewsForRegularUsers (type: `boolean`, default: `false`)
318+
319+
If `true` only admin users have access to view any other user's profile. By default any user can see any other users public profile page.
320+
316321
#### minPasswordRequirements (type: `array`, default: `['lower' => 1, 'digit' => 1, 'upper' => 1]`)
317322

318323
Minimum requirements when a new password is automatically generated.

src/User/Controller/ProfileController.php

+12
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@
1111

1212
namespace Da\User\Controller;
1313

14+
use Da\User\Model\User;
1415
use Da\User\Query\ProfileQuery;
16+
use Da\User\Traits\ModuleAwareTrait;
1517
use Yii;
1618
use yii\base\Module;
1719
use yii\filters\AccessControl;
1820
use yii\web\Controller;
21+
use yii\web\ForbiddenHttpException;
1922
use yii\web\NotFoundHttpException;
2023

2124
class ProfileController extends Controller
2225
{
26+
use ModuleAwareTrait;
27+
2328
protected $profileQuery;
2429

2530
/**
@@ -67,6 +72,13 @@ public function actionIndex()
6772

6873
public function actionShow($id)
6974
{
75+
$user = Yii::$app->user;
76+
/** @var User $identity */
77+
$identity = $user->getIdentity();
78+
if($user->getId() != $id && $this->module->disableProfileViewsForRegularUsers && !$identity->getIsAdmin()) {
79+
throw new ForbiddenHttpException();
80+
}
81+
7082
$profile = $this->profileQuery->whereUserId($id)->one();
7183

7284
if ($profile === null) {

src/User/Module.php

+4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ class Module extends BaseModule
241241
* @var boolean whether to disable IP logging into user table
242242
*/
243243
public $disableIpLogging = false;
244+
/**
245+
* @var boolean whether to disable viewing any user's profile for non-admin users
246+
*/
247+
public $disableProfileViewsForRegularUsers = false;
244248
/**
245249
* @var array Minimum requirements when a new password is automatically generated.
246250
* Array structure: `requirement => minimum number characters`.

0 commit comments

Comments
 (0)