File:
0.00.1a/core/ContextManager.php (
View as Code)
1: 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: if ($sid=="")
225: {
226: $session=$this->SessionManager->Create($type, $username, $password);
227: if (!$session) // session creation failed
228: {
229: $this->DESK->LoggingEngine->Log("Session Creation Failed for Customer ".$username, "Context", "Fail", 4);
230: return false;
231: }
232: else
233: {
234: $this->DESK->LoggingEngine->Log("Session Created for Customer ".$username, "Context", "Open", 9);
235: $this->type = $type;
236: $this->open = true;
237: $this->Session=$session;
238: $this->BuildMenuItems();
239: return true;
240: }
241: }
242: else // pre-existing session
243: {
244: $session=$this->SessionManager->Check($sid);
245: if (!$session)
246: {
247: $this->DESK->LoggingEngine->Log("Session Check Failed for SID ".$sid, "Context", "Fail", 9);
248: return false;
249: }
250: else
251: {
252: $this->DESK->LoggingEngine->Log("Session Check for Customer ".$session->username, "Context", "Check", 9);
253: $this->type = $type;
254: $this->open = true;
255: $this->Session=$session;
256: $this->BuildMenuItems();
257: return true;
258: }
259: }
260: }
261: return false;
262: }
263:
264: /**
265: * Close the open context
266: **/
267: function Close()
268: {
269: $this->Session = null;
270: $this->type = ContextType::None;
271: $this->open = false;
272: }
273:
274: /**
275: * Destroy the context (logout action)
276: **/
277: function Destroy()
278: {
279: if (!$this->open)
280: return false;
281: $this->SessionManager->Destroy($this->Session->sid);
282: }
283:
284: /**
285: * Get menu items for current context
286: * @return mixed menu item description array or bool false for no menu
287: **/
288: function MenuItems()
289: {
290: if (!$this->open)
291: return false;
292:
293: return $this->MenuItems;
294: }
295:
296:
297: /**
298: * Add a menu item (if tag exists will append otherwise will create as new)
299: **/
300: function AddMenuItem($tag, $item)
301: {
302: if (isset($this->MenuItems[$tag]))
303: $this->MenuItems[$tag]->submenu[] = $item;
304: else
305: $this->MenuItems[$tag] = $item;
306: }
307:
308: /**
309: * Build menu items list
310: **/
311: function BuildMenuItems()
312: {
313:
314: $home = new MenuItem();
315: $home->tag="home";
316: $home->onclick="DESK.displayMain(true);";
317: $home->display="Home";
318:
319: $myreq = new MenuItem();
320: $myreg->tag="myreq";
321: $myreq->onclick="DESK.displayMain(true); DESK.mainPane(0, '".$this->Session->username."');";
322: $myreq->display="My Requests";
323: $home->submenu[]=$myreq;
324:
325: $this->AddMenuItem("home", $home);
326:
327: $user = new MenuItem();
328: $user->tag="request";
329: $user->display="Requests";
330:
331: $classes = $this->DESK->RequestManager->GetRequestClasses();
332:
333: foreach($classes as $id => $class)
334: {
335: $req = new MenuItem();
336: $req->tag="newrequest_".$id;
337: $req->display="New ".$class['classname'];
338: $req->onclick="DESK.createRequest(".$id.");";
339: $user->submenu[]=$req;
340: }
341:
342: $this->AddMenuItem("request", $user);
343:
344: $entity = new MenuItem();
345: $entity->tag="entity";
346: $entity->display="Entity";
347:
348: foreach($this->DESK->DataDictionary->Tables as $table)
349: {
350: if ($table->editable)
351: {
352: $add = new MenuItem();
353: $add->tag=$table->entity."_create";
354: $name = $table->name == "" ? $table->entity : $table->name;
355: $add->display="Add ".$name;
356: $add->onclick="DESK.entityCreate('".$table->entity."');";
357: $entity->submenu[]=$add;
358:
359: $search = new MenuItem();
360: $search->tag=$table->entity."_search";
361: $search->display="Search ".$name;
362: $search->onclick="DESK.entitySearch('".$table->entity."');";
363: $entity->submenu[]=$search;
364: }
365: }
366:
367: $this->AddMenuItem("entity", $entity);
368:
369: $sys = new MenuItem();
370: $sys->tag="system";
371: $sys->display="System";
372:
373: $i = new MenuItem();
374: $i->tag="sysadmin";
375: $i->display="System Settings";
376: $i->onclick="DESK.loadSubpage('sysadmin');";
377: $sys->submenu[]=$i;
378:
379: $relogin = new MenuItem();
380: $relogin->tag="relogin";
381: $relogin->display="Relogin";
382: $relogin->onclick="DESK.relogin();";
383: $sys->submenu[]=$relogin;
384:
385: $mobile = new MenuItem();
386: $mobile->tag="mobile";
387: $mobile->display="Mobile Interface";
388: $mobile->onclick="DESK.goMobile();";
389: $sys->submenu[]=$mobile;
390:
391: $logout = new MenuItem();
392: $logout->tag="logout";
393: $logout->display="Logout";
394: $logout->onclick="DESK.logout_click();";
395: $sys->submenu[]=$logout;
396:
397: $this->AddMenuItem("system", $sys);
398:
399: $pages = new MenuItem();
400: $pages->tag="pages";
401: $pages->display="Pages";
402:
403: $debug = new MenuItem();
404: $debug->tag="debug";
405: $debug->display="Debug";
406: $debug->onclick="DESK.loadSubpage('debug');";
407: $pages->submenu[]=$debug;
408:
409: $this->AddMenuItem("pages", $pages);
410:
411: $this->DESK->PluginManager->BuildMenu();
412: }
413:
414: }
415: ?>
416: