SessionController added/updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / controller / SessionController.java
CommitLineData
f0b6a43e
M
1/*\r
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
3 *\r
4 * Project Smart Reservation System.\r
5 *\r
6 * Distributable under GPL license. See terms of license at gnu.org.\r
7 */\r
8package ch.bfh.ti.srs.controller;\r
9\r
f0b6a43e
M
10import java.util.HashSet;\r
11import java.util.Set;\r
12\r
13import ch.bfh.ti.srs.datahandler.DataHandler;\r
14import ch.bfh.ti.srs.datahandler.DataHandlerForTests;\r
a05f995c 15import ch.bfh.ti.srs.dataschema.Role;\r
f0b6a43e
M
16import ch.bfh.ti.srs.dataschema.User;\r
17\r
18public class SessionController {\r
19 private Set<Controller> controllers;\r
20 private DataHandler dh;\r
21 private User currentUser; \r
22\r
30d10517
M
23 public SessionController(DataHandler dh){\r
24 this.dh = dh;\r
25 controllers = new HashSet<Controller>();\r
26 }\r
27 \r
f0b6a43e 28 public SessionController(){\r
30d10517 29 this.dh = new DataHandlerForTests();\r
f0b6a43e
M
30 controllers = new HashSet<Controller>();\r
31 }\r
32 \r
33 /**\r
34 * Tries to log in a <code>User</code> with the given credentials\r
35 * @param userName\r
36 * user name\r
37 * @param password\r
38 * the stored password for the given user name\r
39 * @return <code>True</code> if the user could log in successfully,\r
40 * <code>False</code> either the username or the password was not\r
41 * correct.\r
42 * @throws Exception\r
43 * no <code>DataHandler</code> set\r
f0b6a43e
M
44 */\r
45 public Set<Controller> logIn(String userName, String password) throws Exception {\r
46 if (dh == null)\r
47 throw new Exception("No DataHandler set");\r
48 if (currentUser != null)\r
49 throw new Exception("A user is already logged in");\r
50 User tempUser = dh.findUser(userName);\r
51 if (tempUser != null || tempUser.checkPassword(password)){\r
52 currentUser = tempUser;\r
30d10517 53 controllers.clear();\r
a05f995c 54 \r
a05f995c
M
55 for (Role role : currentUser.getRoles()) {\r
56 switch(role){\r
57 case User:\r
30d10517 58 controllers.add(new UserController(dh, this));\r
a05f995c 59 case ResourceManager:\r
30d10517 60 controllers.add(new ResourceManagerController(dh, this));\r
a05f995c 61 case Admin:\r
30d10517 62 //Add AdminController to controllers\r
a05f995c
M
63 }\r
64 }\r
f0b6a43e 65 }\r
30d10517
M
66 else{\r
67 throw new Exception("Username or password wrong");\r
68 }\r
f0b6a43e
M
69 return controllers;\r
70 }\r
71\r
72 /**\r
73 * Logs out the current logged user\r
74 * @throws Exception\r
75 * either no <code>DataHandler</code> is set or no user is\r
76 * logged in\r
77 */\r
78 public void logOut() throws Exception {\r
79 if (dh == null)\r
80 throw new Exception("No DataHandler set");\r
81 if (currentUser == null)\r
82 throw new Exception("No user is logged in");\r
83 currentUser = null;\r
9608c1e8 84 for (Controller controller : controllers) {\r
a05f995c 85 controller = null;\r
9608c1e8 86 }\r
f0b6a43e
M
87 controllers.clear();\r
88 }\r
89\r
90 /**\r
91 * Gets the user name of the current user\r
92 * @deprecated use <code>getLoggedUser().getUserName()</code>\r
93 * @return user name\r
94 * @throws Exception\r
95 * no <code>DataHandler</code> is set\r
96 */\r
97 @Deprecated\r
98 public String getLoggedUserName() throws Exception {\r
99 if (currentUser == null)\r
100 throw new Exception("No user is logged in");\r
101 return currentUser.getUserName();\r
102 }\r
103\r
104 /**\r
105 * Gets the mail address of the current user\r
106 * @deprecated use <code>getLoggedUser().getMail()</code>\r
107 * @return Mail address\r
108 * @throws Exception\r
109 * no user is logged in\r
110 */\r
111 @Deprecated\r
112 public String getLoggedMail() throws Exception {\r
113 if (currentUser == null)\r
114 throw new Exception("No user is logged in");\r
115 return currentUser.getMail();\r
116 }\r
117\r
118 /**\r
119 * Gets the first name of the current user\r
120 * @deprecated use <code>getLoggedUser().getName()</code>\r
121 * @return First name\r
122 * @throws Exception\r
123 * no user is logged in\r
124 */\r
125 @Deprecated\r
126 public String getLoggedName() throws Exception {\r
127 if (currentUser == null)\r
128 throw new Exception("No user is logged in");\r
129 return currentUser.getName();\r
130 }\r
131\r
132 /**\r
133 * Gets the last name of the current user\r
134 * @deprecated use <code>getLoggedUser().getLastName()</code>\r
135 * @return Last name\r
136 * @throws Exception\r
137 * no user is logged in\r
138 */\r
139 @Deprecated\r
140 public String getLoggedLastname() throws Exception {\r
141 if (currentUser == null)\r
142 throw new Exception("No user is logged in");\r
143 return currentUser.getLastname();\r
144 }\r
145\r
146 /**\r
147 * Gets the current logged <code>User</code>\r
148 * @return current <code>User</code>, or <code>null</code> if no\r
149 * <code>User</code> is logged in\r
150 */\r
151 public User getLoggedUser() {\r
152 if (currentUser == null)\r
153 return null;\r
154 return currentUser;\r
155 }\r
156\r
157}\r