diff --git a/Changelog b/Changelog index 78910c0d7..c03881d00 100755 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +2018-08-06 DariusIII + * Chg: Update handling of user and role disabled categories 2018-08-05 DariusIII * Chg: Update Controllers and templates for user related permission changes * Chg: Add custom ClearanceMiddleware diff --git a/app/Http/Controllers/BasePageController.php b/app/Http/Controllers/BasePageController.php index 4e878a7ca..50c9f0845 100644 --- a/app/Http/Controllers/BasePageController.php +++ b/app/Http/Controllers/BasePageController.php @@ -11,7 +11,6 @@ use App\Models\Category; use App\Models\Settings; use Blacklight\Contents; use App\Models\Forumpost; -use App\Models\RoleExcludedCategory; use Illuminate\Support\Facades\Auth; use Illuminate\Pagination\LengthAwarePaginator; @@ -225,7 +224,6 @@ class BasePageController extends Controller protected function setUserPreferences(): void { $this->userdata['categoryexclusions'] = User::getCategoryExclusion(Auth::id()); - $this->userdata['rolecategoryexclusions'] = RoleExcludedCategory::getRoleCategoryExclusion($this->userdata['roles_id']); // Change the theme to user's selected theme if they selected one, else use the admin one. if ((int) Settings::settingValue('site.main.userselstyle') === 1) { diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index e2dec4704..c4220d4a1 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -13,7 +13,6 @@ use App\Models\UserDownload; use Illuminate\Http\Request; use App\Models\ReleaseComment; use Blacklight\utility\Utility; -use App\Models\UserExcludedCategory; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Mail; use Jrean\UserVerification\Facades\UserVerification; @@ -90,7 +89,6 @@ class ProfileController extends BasePageController $this->smarty->assign( [ 'commentslist' => ReleaseComment::getCommentsForUserRange($userID), - 'exccats' => implode(',', UserExcludedCategory::getCategoryExclusionNames($userID)), 'saburl' => $sab->url, 'sabapikey' => $sab->apikey, 'sabapikeytype' => $sab->apikeytype !== '' ? $sabApiKeyTypes[$sab->apikeytype] : '', diff --git a/app/Models/Category.php b/app/Models/Category.php index 3ef39aa70..139594c27 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -19,8 +19,6 @@ use Illuminate\Database\Eloquent\Model; * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Category[] $children * @property-read \App\Models\Category|null $parent * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Release[] $releases - * @property-read \App\Models\RoleExcludedCategory $roleExcludedCategory - * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserExcludedCategory[] $userExcludedCategory * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDescription($value) * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDisablepreview($value) * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereId($value) @@ -203,21 +201,6 @@ class Category extends Model return $this->hasMany(static::class, 'parentid'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function userExcludedCategory() - { - return $this->hasMany(UserExcludedCategory::class, 'categories_id'); - } - - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function roleExcludedCategory() - { - return $this->belongsTo(RoleExcludedCategory::class, 'categories_id'); - } /** * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] @@ -452,7 +435,7 @@ class Category extends Model { $ret = []; - $arr = self::query()->remember(config('nntmux.cache_expiry_long'))->where('status', '=', self::STATUS_ACTIVE)->get()->toArray(); + $arr = self::query()->remember(config('nntmux.cache_expiry_long'))->where('status', '=', self::STATUS_ACTIVE)->get(['id', 'title', 'parentid'])->toArray(); foreach ($arr as $key => $val) { if ($val['id'] === self::OTHER_ROOT) { diff --git a/app/Models/RoleExcludedCategory.php b/app/Models/RoleExcludedCategory.php deleted file mode 100644 index cd09972fb..000000000 --- a/app/Models/RoleExcludedCategory.php +++ /dev/null @@ -1,77 +0,0 @@ -belongsTo(Role::class, 'roles_id'); - } - - public function category() - { - return $this->hasMany(Category::class, 'categories_id'); - } - - /** - * @param $role - * - * @return array - */ - public static function getRoleCategoryExclusion($role): array - { - $ret = []; - $categories = self::query()->where('roles_id', $role)->get(['categories_id']); - foreach ($categories as $category) { - $ret[] = $category['categories_id']; - } - - return $ret; - } - - /** - * @param $role - * @param $catids - */ - public static function addRoleCategoryExclusions($role, array $catids): void - { - self::delRoleCategoryExclusions($role); - if (\count($catids) > 0) { - foreach ($catids as $catid) { - self::create(['roles_id' => $role, 'categories_id' => $catid]); - } - } - } - - /** - * @param $role - */ - public static function delRoleCategoryExclusions($role): void - { - self::query()->where('roles_id', $role)->delete(); - } -} diff --git a/app/Models/User.php b/app/Models/User.php index 985878bd3..c7f70cbfa 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -60,7 +60,6 @@ use Illuminate\Foundation\Auth\User as Authenticatable; * @property string|null $remember_token * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleaseComment[] $comment * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDownload[] $download - * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserExcludedCategory[] $excludedCategory * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\DnzbFailure[] $failedRelease * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Invitation[] $invitation * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications @@ -207,14 +206,6 @@ class User extends Authenticatable return $this->hasMany(DnzbFailure::class, 'users_id'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function excludedCategory() - { - return $this->hasMany(UserExcludedCategory::class, 'users_id'); - } - /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ @@ -935,14 +926,52 @@ class User extends Authenticatable public static function getCategoryExclusion($userID): array { $ret = []; - $categories = self::query()->where('id', $userID)->first(); - if ($categories !== null) { - foreach ($categories->excludedCategory as $category) { - $ret[] = $category['categories_id']; + + $user = self::find($userID); + + $userAllowed = $user->getDirectPermissions()->pluck('name')->toArray(); + $roleAllowed = $user->getAllPermissions()->pluck('name')->toArray(); + + $allowed = array_intersect($roleAllowed, $userAllowed); + + $cats = ['view console', 'view movies', 'view audio', 'view tv', 'view pc', 'view adult', 'view books', 'view other']; + + if (! empty($allowed)) { + foreach ($cats as $cat) { + if (! \in_array($cat, $allowed, false)) { + switch ($cat) { + case 'view console': + $ret[] = 1000; + continue 2; + case 'view movies': + $ret[] = 2000; + continue 2; + case 'view audio': + $ret[] = 3000; + continue 2; + case 'view tv': + $ret[] = 4000; + continue 2; + case 'view pc': + $ret[] = 5000; + continue 2; + case 'view adult': + $ret[] = 6000; + continue 2; + case 'view books': + $ret[] = 7000; + continue 2; + case 'view other': + $ret[] = 1; + + } + } } } - return $ret; + $exclusion = Category::query()->whereIn('parentid', $ret)->pluck('id')->toArray(); + + return $exclusion; } /** diff --git a/app/Models/UserExcludedCategory.php b/app/Models/UserExcludedCategory.php deleted file mode 100644 index dbcd15749..000000000 --- a/app/Models/UserExcludedCategory.php +++ /dev/null @@ -1,103 +0,0 @@ -belongsTo(User::class, 'users_id'); - } - - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function category() - { - return $this->belongsTo(Category::class, 'categories_id'); - } - - /** - * @param $uid - */ - public static function delUserCategoryExclusions($uid): void - { - self::query()->where('users_id', $uid)->delete(); - } - - /** - * @param $uid - * @param array $catids - */ - public static function addCategoryExclusions($uid, array $catids): void - { - self::delUserCategoryExclusions($uid); - if (\count($catids) > 0) { - foreach ($catids as $catid) { - self::create(['users_id' => $uid, 'categories_id' => $catid]); - } - } - } - - /** - * Get list of category names excluded by the user. - * - * @param int $userID ID of the user. - * - * @return array - * @throws \Exception - */ - public static function getCategoryExclusionNames($userID): array - { - $categories = self::with('category')->where('users_id', $userID)->get(); - $ret = []; - if ($categories !== null) { - foreach ($categories as $cat) { - $ret[] = $cat->category->title; - } - } - - return $ret; - } - - /** - * @param $uid - * @param $catid - */ - public static function delCategoryExclusion($uid, $catid): void - { - self::query()->where(['users_id'=> $uid, 'categories_id' => $catid])->delete(); - } -}