Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Ryuske
Created May 10, 2016 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryuske/015335b4c1ac1db85ab70ad0b29dfb3f to your computer and use it in GitHub Desktop.
Save Ryuske/015335b4c1ac1db85ab70ad0b29dfb3f to your computer and use it in GitHub Desktop.
Library of user functions
<?php
/**
* File for App\Models\User\Shared
*/
namespace App\Models\User;
/**
* Helper methods for getting information about users
*
* Class Shared
* @package App\Models\User
*/
class Shared {
/**
* Used to find out if the currently logged in user is an admin
*
* @return bool
*/
static public function isAdmin() {
if (\Auth::check() && 'a' === \Auth::user()->privileges) {
return true;
}
return false;
}
/**
* Used to determine if an account has been associated with wepay yet or not
*
* @param $user
* @return bool
*/
static public function hasWepay($user) {
if (empty($user->wepay_id) || 0 === $user->wepay_id) {
return false;
}
return true;
}
/**
* Get the pages available to the current user
*
* @param string $type
* @return array|string
*/
static public function getPages($type='array') {
$pages = ('array' === $type) ? [] : '';
if (\Auth::check()) {
// If the user is an admin, show all pages
if (self::isAdmin()) {
foreach (\Page::all() as $page) {
// Do I want to use the pages?
if ('array' === $type) {
$pages[$page->id] = $page->title;
// Or do I just want the ID for validation?
} else {
$pages .= ',' . $page->id;
}
}
// As long as the user isn't an admin, show the pages added to their account
} elseif (\Auth::user()->portals) {
foreach (unserialize(\Auth::user()->portals) as $page) {
$page = \Page::find($page);
// Does the page even exist?
if (NULL !== $page) {
// Do I want to use the pages?
if ('array' === $type) {
$pages[$page->id] = $page->title;
// Or do I just want the ID for validation?
} else {
$pages .= ',' . $page->id;
}
}
}
}
}
return $pages;
}
/**
* Get the favorites of the specified user (or if no user is specified the currently logged in user)
*
* @param null $user_id
* @return array|mixed
*/
static public function getFavorites($user_id=NULL) {
if (NULL === $user_id && \Auth::check()) {
$favorites = unserialize(\Auth::user()->favorites);
return (0 != $favorites) ? $favorites : [];
} else {
$user = \User::find($user_id);
if (NULL !== $user) {
$favorites = unserialize($user->favorites);
return (0 != $favorites) ? $favorites : [];
}
}
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment