Viewing File core/ContextManager.php of 0.00.0a
|
1: <?php 2: /* ------------------------------------------------------------- 3: This file is part of FreeDESK 4: 5: FreeDESK is (C) Copyright 2012 David Cutting 6: 7: FreeDESK is free software: you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation, either version 3 of the License, or 10: (at your option) any later version. 11: 12: FreeDESK is distributed in the hope that it will be useful, 13: but WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15: GNU General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with FreeDESK. If not, see www.gnu.org/licenses 19: 20: For more information see www.purplepixie.org/freedesk/ 21: -------------------------------------------------------------- */ 22: 23: /** 24: * Context Connection Types 25: **/ 26: abstract class ContextType 27: { 28: /** 29: * None 30: **/ 31: const None = -1; 32: /** 33: * System 34: **/ 35: const System = 0; 36: /** 37: * User 38: **/ 39: const User = 1; 40: /** 41: * Customer 42: **/ 43: const Customer = 2; 44: } 45: 46: /** 47: * Menu Items 48: **/ 49: class MenuItem 50: { 51: /** 52: * Tag Name 53: **/ 54: var $tag = ""; 55: /** 56: * Display Name 57: **/ 58: var $display = ""; 59: /** 60: * Link 61: **/ 62: var $link = "#"; 63: /** 64: * Onclick event 65: **/ 66: var $onclick = ""; 67: /** 68: * Sub-Menu 69: **/ 70: var $submenu = array(); 71: } 72: 73: /** 74: * Context Manager for FreeDESK - handle connections to the system 75: **/ 76: class ContextManager 77: { 78: /** 79: * FreeDESK instance 80: **/ 81: private $DESK = null; 82: 83: /** 84: * Open Context Flag 85: **/ 86: private $open = false; 87: 88: /** 89: * Context Type 90: **/ 91: private $type = ContextType::None; 92: 93: /** 94: * Current Session 95: **/ 96: var $Session = null; 97: 98: /** 99: * Menu Items 100: **/ 101: var $MenuItems = array(); 102: 103: /** 104: * Session Manager 105: **/ 106: private $SessionManager = null; 107: 108: /** 109: * Constructor 110: * @param mixed $freeDESK FreeDESK instance 111: **/ 112: function ContextManager(&$freeDESK) 113: { 114: $this->DESK = &$freeDESK; 115: $this->DESK->PluginManager->Register(new Plugin( 116: "Context Manager", "0.01", "Core" )); 117: // Load the SessionManager 118: $this->SessionManager = $this->DESK->Include->IncludeInstance("core/SessionManager.php","SessionManager"); 119: 120: // Our permissions 121: $this->DESK->PermissionManager->Register("user_admin",false); 122: $this->DESK->PermissionManager->Register("sysadmin_advanced",false); 123: } 124: 125: /** 126: * Check if context is open 127: * @return bool open flag 128: **/ 129: function IsOpen() 130: { 131: return $this->open; 132: } 133: 134: /** 135: * Get the current type of open context 136: **/ 137: function GetType() 138: { 139: if (!$this->open) 140: return ContextType::None; // should be that if closed but to be safe 141: return $this->type; 142: } 143: 144: /** 145: * Check if has permission 146: * @param string $perm Permission type requested 147: * @return bool flag indicating if context has permission 148: **/ 149: function Permission($perm) 150: { 151: if (!$this->open) 152: return false; // no open context 153: else if ($this->type == ContextType::None) 154: return false; // no context type 155: else if ($this->type == ContextType::System) 156: return true; // system context - all permissions 157: else if ($this->type == ContextType::User) 158: { 159: return $this->DESK->PermissionManager->UserPermission($perm, $this->Session->username); 160: } 161: else if ($this->type == ContextType::Customer) 162: return true; // TO BE IMPLEMENTED WITH PermissionManager 163: else 164: return false; // default - refused 165: } 166: 167: /** 168: * Open a context 169: * @param mixed $type Context type (from ContextType consts) 170: * @param string $sid Session ID if existing session (optional) 171: * @param string $username Username (optional) 172: * @param string $password Password (optional) 173: * @return bool returns true if successful or false on failure 174: **/ 175: function Open($type, $sid="", $username="", $password="") 176: { 177: if ($type == ContextType::System) 178: { 179: $this->type = $type; 180: $this->open = true; 181: return true; 182: } 183: else if ($type == ContextType::User) 184: { 185: if ($sid=="") 186: { 187: $session=$this->SessionManager->Create($type, $username, $password); 188: if (!$session) // session creation failed 189: { 190: $this->DESK->LoggingEngine->Log("Session Creation Failed for User ".$username, "Context", "Fail", 4); 191: return false; 192: } 193: else 194: { 195: $this->DESK->LoggingEngine->Log("Session Created for User ".$username, "Context", "Open", 9); 196: $this->type = $type; 197: $this->open = true; 198: $this->Session=$session; 199: $this->BuildMenuItems(); 200: return true; 201: } 202: } 203: else // pre-existing session 204: { 205: $session=$this->SessionManager->Check($sid); 206: if (!$session) 207: { 208: $this->DESK->LoggingEngine->Log("Session Check Failed for SID ".$sid, "Context", "Fail", 9); 209: return false; 210: } 211: else 212: { 213: $this->DESK->LoggingEngine->Log("Session Check for User ".$session->username, "Context", "Check", 9); 214: $this->type = $type; 215: $this->open = true; 216: $this->Session=$session; 217: $this->BuildMenuItems(); 218: return true; 219: } 220: } 221: } 222: else if ($type == ContextType::Customer) 223: { 224: // 225: } 226: return false; 227: } 228: 229: /** 230: * Close the open context 231: **/ 232: function Close() 233: { 234: $this->Session = null; 235: $this->type = ContextType::None; 236: $this->open = false; 237: } 238: 239: /** 240: * Destroy the context (logout action) 241: **/ 242: function Destroy() 243: { 244: if (!$this->open) 245: return false; 246: $this->SessionManager->Destroy($this->Session->sid); 247: } 248: 249: /** 250: * Get menu items for current context 251: * @return mixed menu item description array or bool false for no menu 252: **/ 253: function MenuItems() 254: { 255: if (!$this->open) 256: return false; 257: 258: return $this->MenuItems; 259: } 260: 261: 262: /** 263: * Add a menu item (if tag exists will append otherwise will create as new) 264: **/ 265: function AddMenuItem($tag, $item) 266: { 267: if (isset($this->MenuItems[$tag])) 268: $this->MenuItems[$tag]->submenu[] = $item; 269: else 270: $this->MenuItems[$tag] = $item; 271: } 272: 273: /** 274: * Build menu items list 275: **/ 276: function BuildMenuItems() 277: { 278: 279: $home = new MenuItem(); 280: $home->tag="home"; 281: $home->onclick="DESK.displayMain(true);"; 282: $home->display="Home"; 283: 284: $myreq = new MenuItem(); 285: $myreg->tag="myreq"; 286: $myreq->onclick="DESK.displayMain(true); DESK.mainPane(0, '".$this->Session->username."');"; 287: $myreq->display="My Requests"; 288: $home->submenu[]=$myreq; 289: 290: $this->AddMenuItem("home", $home); 291: 292: $user = new MenuItem(); 293: $user->tag="request"; 294: $user->display="Requests"; 295: 296: $req = new MenuItem(); 297: $req->tag="newrequest"; 298: $req->display="New Request"; 299: $req->onclick="DESK.createRequest();"; 300: $user->submenu[]=$req; 301: 302: $this->AddMenuItem("request", $user); 303: 304: $entity = new MenuItem(); 305: $entity->tag="entity"; 306: $entity->display="Entity"; 307: 308: foreach($this->DESK->DataDictionary->Tables as $table) 309: { 310: if ($table->editable) 311: { 312: $add = new MenuItem(); 313: $add->tag=$table->entity."_create"; 314: $name = $table->name == "" ? $table->entity : $table->name; 315: $add->display="Add ".$name; 316: $add->onclick="DESK.entityCreate('".$table->entity."');"; 317: $entity->submenu[]=$add; 318: 319: $search = new MenuItem(); 320: $search->tag=$table->entity."_search"; 321: $search->display="Search ".$name; 322: $search->onclick="DESK.entitySearch('".$table->entity."');"; 323: $entity->submenu[]=$search; 324: } 325: } 326: 327: $this->AddMenuItem("entity", $entity); 328: 329: $sys = new MenuItem(); 330: $sys->tag="system"; 331: $sys->display="System"; 332: 333: $i = new MenuItem(); 334: $i->tag="sysadmin"; 335: $i->display="System Settings"; 336: $i->onclick="DESK.loadSubpage('sysadmin');"; 337: $sys->submenu[]=$i; 338: 339: $relogin = new MenuItem(); 340: $relogin->tag="relogin"; 341: $relogin->display="Relogin"; 342: $relogin->onclick="DESK.relogin();"; 343: $sys->submenu[]=$relogin; 344: 345: $logout = new MenuItem(); 346: $logout->tag="logout"; 347: $logout->display="Logout"; 348: $logout->onclick="DESK.logout_click();"; 349: $sys->submenu[]=$logout; 350: 351: $this->AddMenuItem("system", $sys); 352: 353: $pages = new MenuItem(); 354: $pages->tag="pages"; 355: $pages->display="Pages"; 356: 357: $debug = new MenuItem(); 358: $debug->tag="debug"; 359: $debug->display="Debug"; 360: $debug->onclick="DESK.loadSubpage('debug');"; 361: $pages->submenu[]=$debug; 362: 363: $this->AddMenuItem("pages", $pages); 364: 365: $this->DESK->PluginManager->BuildMenu(); 366: } 367: 368: } 369: ?> 370: