Roles added
[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
10import java.rmi.AlreadyBoundException;\r
11import java.util.HashSet;\r
9608c1e8 12import java.util.Map;\r
f0b6a43e
M
13import java.util.Set;\r
14\r
15import ch.bfh.ti.srs.datahandler.DataHandler;\r
16import ch.bfh.ti.srs.datahandler.DataHandlerForTests;\r
17import ch.bfh.ti.srs.dataschema.User;\r
18\r
19public class SessionController {\r
20 private Set<Controller> controllers;\r
21 private DataHandler dh;\r
22 private User currentUser; \r
23\r
24 public SessionController(){\r
25 dh = new DataHandlerForTests();\r
26 controllers = new HashSet<Controller>();\r
27 }\r
28 \r
29 /**\r
30 * Tries to log in a <code>User</code> with the given credentials\r
31 * @param userName\r
32 * user name\r
33 * @param password\r
34 * the stored password for the given user name\r
35 * @return <code>True</code> if the user could log in successfully,\r
36 * <code>False</code> either the username or the password was not\r
37 * correct.\r
38 * @throws Exception\r
39 * no <code>DataHandler</code> set\r
40 * @throws AlreadyBoundException\r
41 * a <code>User</code> is already logged in\r
42 */\r
43 public Set<Controller> logIn(String userName, String password) throws Exception {\r
44 if (dh == null)\r
45 throw new Exception("No DataHandler set");\r
46 if (currentUser != null)\r
47 throw new Exception("A user is already logged in");\r
48 User tempUser = dh.findUser(userName);\r
49 if (tempUser != null || tempUser.checkPassword(password)){\r
50 currentUser = tempUser;\r
51 //TODO: Check for userRoles, add new controller object for each\r
52 }\r
53 return controllers;\r
54 }\r
55\r
56 /**\r
57 * Logs out the current logged user\r
58 * @throws Exception\r
59 * either no <code>DataHandler</code> is set or no user is\r
60 * logged in\r
61 */\r
62 public void logOut() throws Exception {\r
63 if (dh == null)\r
64 throw new Exception("No DataHandler set");\r
65 if (currentUser == null)\r
66 throw new Exception("No user is logged in");\r
67 currentUser = null;\r
9608c1e8
M
68 for (Controller controller : controllers) {\r
69 \r
70 }\r
f0b6a43e
M
71 controllers.clear();\r
72 }\r
73\r
74 /**\r
75 * Gets the user name of the current user\r
76 * @deprecated use <code>getLoggedUser().getUserName()</code>\r
77 * @return user name\r
78 * @throws Exception\r
79 * no <code>DataHandler</code> is set\r
80 */\r
81 @Deprecated\r
82 public String getLoggedUserName() throws Exception {\r
83 if (currentUser == null)\r
84 throw new Exception("No user is logged in");\r
85 return currentUser.getUserName();\r
86 }\r
87\r
88 /**\r
89 * Gets the mail address of the current user\r
90 * @deprecated use <code>getLoggedUser().getMail()</code>\r
91 * @return Mail address\r
92 * @throws Exception\r
93 * no user is logged in\r
94 */\r
95 @Deprecated\r
96 public String getLoggedMail() throws Exception {\r
97 if (currentUser == null)\r
98 throw new Exception("No user is logged in");\r
99 return currentUser.getMail();\r
100 }\r
101\r
102 /**\r
103 * Gets the first name of the current user\r
104 * @deprecated use <code>getLoggedUser().getName()</code>\r
105 * @return First name\r
106 * @throws Exception\r
107 * no user is logged in\r
108 */\r
109 @Deprecated\r
110 public String getLoggedName() throws Exception {\r
111 if (currentUser == null)\r
112 throw new Exception("No user is logged in");\r
113 return currentUser.getName();\r
114 }\r
115\r
116 /**\r
117 * Gets the last name of the current user\r
118 * @deprecated use <code>getLoggedUser().getLastName()</code>\r
119 * @return Last name\r
120 * @throws Exception\r
121 * no user is logged in\r
122 */\r
123 @Deprecated\r
124 public String getLoggedLastname() throws Exception {\r
125 if (currentUser == null)\r
126 throw new Exception("No user is logged in");\r
127 return currentUser.getLastname();\r
128 }\r
129\r
130 /**\r
131 * Gets the current logged <code>User</code>\r
132 * @return current <code>User</code>, or <code>null</code> if no\r
133 * <code>User</code> is logged in\r
134 */\r
135 public User getLoggedUser() {\r
136 if (currentUser == null)\r
137 return null;\r
138 return currentUser;\r
139 }\r
140\r
141}\r