/** * This is a helper method which will handle necessary data * needed for the view. This is created to centralize this functionality * for all presenters * @param string $template * @param array $data * @param string $parent */ protected function view($template, $data = [], $parent = '') { if ($parent) { $name = $parent; } else { $namespace = get_class($this); $chunks = explode('\\', $namespace); $name = array_pop($chunks); $name = str_replace(PresenterFactory::getSuffix(), '', $name); } //$menu = LibraryFactory::getInstance('Menu')->getMyMenus(); //$this->view->menu = $menu; $templateName = $name . '.' . $template; return view($templateName, $data, (array) $this->view); }
/** * This is a helper method which will handle necessary data * needed for the view. This is created to centralize this functionality * for all presenters * @param string $template * @param array $data * @param string $parent */ protected function view($template, $data = [], $parent = '') { if ($parent) { $name = $parent; } else { $namespace = get_class($this); $chunks = explode('\\', $namespace); $name = array_pop($chunks); $name = str_replace(PresenterFactory::getSuffix(), '', $name); } $menuLib = LibraryFactory::getInstance('Menu'); if (!$menuLib->isActionAllowed($template)) { return view('errors.403'); } $this->view->menu = $menuLib->getMyMenus(); $templateName = $name . '.' . $template; $this->view->isAdmin = $this->isAdmin(); $this->view->isAuditor = $this->isAuditor(); $this->view->isAccounting = $this->isAcounting(); $this->view->isGuest1 = $this->isGuest1(); $this->view->isGuest2 = $this->isGuest2(); return view($templateName, $data, (array) $this->view); }
/** * Check if a specific user has access to this page * @param unknown $page The nav Id or url * @param number $userId The userId */ public function hasPageAccess($page, $userId = 0) { $hasAccess = false; if (!$userId) { $userId = auth()->user() ? auth()->user()->id : 0; } $navModel = ModelFactory::getInstance('Navigation'); if (is_numeric($page)) { $nav = $navModel->find($id); } else { $nav = $navModel->where('url', '=', $page)->first(); } if ($nav) { // Check user permission first // $userToNav = ModelFactory::getInstance('UserToNav') // ->where('user_id','=',$userId) // ->where('nav_id','=',$nav->id) // ->first(); // if($userToNav) // { // return $userToNav->enable; // } // Check role permission $userRoles = ModelFactory::getInstance('User')->with('roles')->find($userId); $roleIds = []; foreach ($userRoles->roles as $role) { $roleIds[] = $role->id; } //@TODO: optimize this $menuLib = LibraryFactory::getInstance('Menu'); foreach ($roleIds as $roleId) { if ($menuLib->roleHasMenu($roleId, $nav->id)) { return true; } } return $hasAccess; } // Finally check feature if (!$hasAccess) { $route = request()->route(); $action = $route->getAction(); $controller = $action['controller']; $namespace = $action['namespace']; if ($controller && $namespace) { $controller = str_replace($namespace . '\\', '', $controller); $chunks = explode('@', $controller); $presenter = $chunks[0]; $method = $chunks[1]; if (false !== strpos(PresenterFactory::getNamespace(), $namespace)) { $name = str_replace(PresenterFactory::getSuffix(), '', $presenter); $permissions = PresenterFactory::getInstance($name)->getPermissions(); } elseif (false !== strpos(ControllerFactory::getNamespace(), $namespace)) { $name = str_replace(ControllerFactory::getSuffix(), '', $presenter); $permissions = ControllerFactory::getInstance($name)->getPermissions(); } elseif (false !== strpos(WebServiceFactory::getNamespace(), $namespace)) { $name = str_replace(WebServiceFactory::getSuffix(), '', $presenter); $permissions = WebServiceFactory::getInstance($name)->getPermissions(); } if (isset($permissions[$method])) { $features = $permissions[$method]; if (!$features || feature_enabled($features)) { return true; } } else { foreach ($permissions as $method => $features) { if (!$features || feature_enabled($features)) { return true; } } } } } return $hasAccess; }