Merge remote-tracking branch 'origin/development'
[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
73469283 21 private User currentUser;\r
f0b6a43e 22\r
73469283 23 public SessionController(DataHandler dh) {\r
30d10517
M
24 this.dh = dh;\r
25 controllers = new HashSet<Controller>();\r
26 }\r
73469283
D
27\r
28 public SessionController() {\r
30d10517 29 this.dh = new DataHandlerForTests();\r
f0b6a43e
M
30 controllers = new HashSet<Controller>();\r
31 }\r
73469283 32\r
f0b6a43e
M
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
f0b6a43e
M
46 if (currentUser != null)\r
47 throw new Exception("A user is already logged in");\r
48 User tempUser = dh.findUser(userName);\r
c1191616
M
49 if (tempUser == null || !tempUser.checkPassword(password))\r
50 throw new Exception("Username or password wrong");\r
73469283 51 else {\r
f0b6a43e 52 currentUser = tempUser;\r
30d10517 53 controllers.clear();\r
73469283 54\r
a05f995c 55 for (Role role : currentUser.getRoles()) {\r
73469283
D
56 switch (role) {\r
57 case User:\r
58 controllers.add(new UserController(dh, this));\r
59 case ResourceManager:\r
60 controllers.add(new ResourceManagerController(dh, this));\r
61 // case Admin:\r
62 // //Add AdminController to controllers\r
a05f995c
M
63 }\r
64 }\r
f0b6a43e
M
65 }\r
66 return controllers;\r
67 }\r
68\r
69 /**\r
70 * Logs out the current logged user\r
71 * @throws Exception\r
72 * either no <code>DataHandler</code> is set or no user is\r
73 * logged in\r
74 */\r
75 public void logOut() throws Exception {\r
76 if (dh == null)\r
77 throw new Exception("No DataHandler set");\r
78 if (currentUser == null)\r
79 throw new Exception("No user is logged in");\r
80 currentUser = null;\r
9608c1e8 81 for (Controller controller : controllers) {\r
a05f995c 82 controller = null;\r
9608c1e8 83 }\r
f0b6a43e
M
84 controllers.clear();\r
85 }\r
86\r
87 /**\r
88 * Gets the user name of the current user\r
89 * @deprecated use <code>getLoggedUser().getUserName()</code>\r
90 * @return user name\r
91 * @throws Exception\r
92 * no <code>DataHandler</code> is set\r
93 */\r
94 @Deprecated\r
95 public String getLoggedUserName() throws Exception {\r
96 if (currentUser == null)\r
97 throw new Exception("No user is logged in");\r
98 return currentUser.getUserName();\r
99 }\r
100\r
101 /**\r
102 * Gets the mail address of the current user\r
103 * @deprecated use <code>getLoggedUser().getMail()</code>\r
104 * @return Mail address\r
105 * @throws Exception\r
106 * no user is logged in\r
107 */\r
108 @Deprecated\r
109 public String getLoggedMail() throws Exception {\r
110 if (currentUser == null)\r
111 throw new Exception("No user is logged in");\r
112 return currentUser.getMail();\r
113 }\r
114\r
115 /**\r
116 * Gets the first name of the current user\r
117 * @deprecated use <code>getLoggedUser().getName()</code>\r
118 * @return First name\r
119 * @throws Exception\r
120 * no user is logged in\r
121 */\r
122 @Deprecated\r
123 public String getLoggedName() throws Exception {\r
124 if (currentUser == null)\r
125 throw new Exception("No user is logged in");\r
126 return currentUser.getName();\r
127 }\r
128\r
129 /**\r
130 * Gets the last name of the current user\r
131 * @deprecated use <code>getLoggedUser().getLastName()</code>\r
132 * @return Last name\r
133 * @throws Exception\r
134 * no user is logged in\r
135 */\r
136 @Deprecated\r
137 public String getLoggedLastname() throws Exception {\r
138 if (currentUser == null)\r
139 throw new Exception("No user is logged in");\r
140 return currentUser.getLastname();\r
141 }\r
142\r
143 /**\r
144 * Gets the current logged <code>User</code>\r
145 * @return current <code>User</code>, or <code>null</code> if no\r
146 * <code>User</code> is logged in\r
147 */\r
148 public User getLoggedUser() {\r
149 if (currentUser == null)\r
150 return null;\r
151 return currentUser;\r
152 }\r
153\r
73469283
D
154 public void registUser(String userName, String password, String email, String name, String lastname)\r
155 throws Exception {\r
c1191616
M
156 if (dh == null)\r
157 throw new Exception("No DataHandler set");\r
158 User newUser = new User(userName, password);\r
159 newUser.setMail(email);\r
160 newUser.setName(name);\r
161 newUser.setLastname(lastname);\r
162 dh.addUser(newUser);\r
163 }\r
f0b6a43e 164}\r