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