Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/3.2/UserRoleManager.cs @ 2211

Last change on this file since 2211 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab 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 * HeuristicLab 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 HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
29using System.Resources;
30using System.Reflection;
31using HeuristicLab.Hive.Contracts;
32
33namespace HeuristicLab.Hive.Server.Core {
34  class UserRoleManager: IUserRoleManager {
35
36    IUserAdapter userAdapter;
37    IUserGroupAdapter userGroupAdapter;
38    IPermissionOwnerAdapter permissionOwnerAdapter;
39
40    public UserRoleManager() {
41      userAdapter = ServiceLocator.GetUserAdapter();
42      userGroupAdapter = ServiceLocator.GetUserGroupAdapter();
43    }
44
45    #region IUserRoleManager Members
46
47    /// <summary>
48    /// returns all users from the database
49    /// </summary>
50    /// <returns></returns>
51    public ResponseList<User> GetAllUsers() {
52      ResponseList<User> response = new ResponseList<User>();
53
54      List<User> allUsers = new List<User>(userAdapter.GetAll());
55      response.List = allUsers;
56      response.Success = true;
57      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERS;
58
59      return response;
60    }
61
62    /// <summary>
63    /// Adds a new user to the database
64    /// </summary>
65    /// <param name="user"></param>
66    /// <returns></returns>
67    public ResponseObject<User> AddNewUser(User user) {
68      ResponseObject<User> response = new ResponseObject<User>();
69
70      if (user.Id != 0) {
71        response.Success = false;
72        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
73        return response;
74      }
75      if (userAdapter.GetByName(user.Name) != null) {
76        response.Success = false;
77        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERNAME_EXISTS_ALLREADY;
78        return response;
79      }
80
81      userAdapter.Update(user);
82      response.Obj = user;
83      response.Success = true;
84      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_SUCCESSFULLY_ADDED;
85     
86      return response;
87    }
88
89    /// <summary>
90    /// returns all usergroups from the database
91    /// </summary>
92    /// <returns></returns>
93    public ResponseList<UserGroup> GetAllUserGroups() {
94      ResponseList<UserGroup> response = new ResponseList<UserGroup>();
95
96      response.List = new List<UserGroup>(userGroupAdapter.GetAll());
97      response.Success = true;
98      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERGROUPS;
99
100      return response;
101    }
102
103    /// <summary>
104    /// Removes a user from the database
105    /// </summary>
106    /// <param name="userId"></param>
107    /// <returns></returns>
108    public Response RemoveUser(long userId) {
109      Response response = new Response();
110      User user = userAdapter.GetById(userId);
111      if (user == null) {
112        response.Success = false;
113        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_DOESNT_EXIST;
114        return response;
115      }
116      userAdapter.Delete(user);
117      response.Success = true;
118      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_REMOVED;
119                         
120      return response;
121    }
122
123    /// <summary>
124    /// Adds a new usergroup to the database
125    /// </summary>
126    /// <param name="userGroup"></param>
127    /// <returns></returns>
128    public ResponseObject<UserGroup> AddNewUserGroup(UserGroup userGroup) {
129      ResponseObject<UserGroup> response = new ResponseObject<UserGroup>();
130     
131      if (userGroup.Id != 0) {
132        response.Success = false;
133        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
134        return response;
135      }
136
137      userGroupAdapter.Update(userGroup);
138      response.Obj = userGroup;
139      response.Success = false;
140      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
141
142      return response;
143    }
144
145    /// <summary>
146    /// Removes a user group from the database
147    /// </summary>
148    /// <param name="groupId"></param>
149    /// <returns></returns>
150    public Response RemoveUserGroup(long groupId) {
151      Response response = new Response();
152
153      UserGroup userGroupFromDb = userGroupAdapter.GetById(groupId);
154      if (userGroupFromDb == null) {
155        response.Success = false;
156        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
157        return response;
158      }
159      userGroupAdapter.Delete(userGroupFromDb);
160      response.Success = false;
161      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
162
163      return response;
164    }
165
166    /// <summary>
167    /// Adds a user into a user group
168    /// </summary>
169    /// <param name="groupId"></param>
170    /// <param name="userId"></param>
171    /// <returns></returns>
172    public Response AddUserToGroup(long groupId, long userId) {
173      Response response = new Response();
174
175      User user = userAdapter.GetById(userId);
176      if (user == null) {
177        response.Success = false;
178        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_DOESNT_EXIST;
179        return response;
180      }
181
182      UserGroup userGroup = userGroupAdapter.GetById(groupId);
183      if (userGroup == null) {
184        response.Success = false;
185        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
186        return response;
187      }
188      userGroup.Members.Add(user);
189      userGroupAdapter.Update(userGroup);
190
191      response.Success = true;
192      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_ADDED;
193
194      return response;
195    }
196
197    /// <summary>
198    /// Adds a user group to a user group
199    /// </summary>
200    /// <param name="groupId"></param>
201    /// <param name="groupToAddId"></param>
202    /// <returns></returns>
203    public Response AddUserGroupToGroup(long groupId, long groupToAddId) {
204      Response response = new Response();
205
206      UserGroup userGroup = userGroupAdapter.GetById(groupId);
207      UserGroup userGroupToAdd = userGroupAdapter.GetById(groupToAddId);
208
209      if (userGroup == null || userGroupToAdd == null) {
210        response.Success = false;
211        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
212        return response;
213      }
214      userGroup.Members.Add(userGroupToAdd);
215      response.Success = true;
216      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED_TO_USERGROUP;
217
218      return response;
219    }
220
221    /// <summary>
222    /// Removes a permission owner (user, user group) from a user group
223    /// </summary>
224    /// <param name="groupId"></param>
225    /// <param name="permissionOwnerId"></param>
226    /// <returns></returns>
227    public Response RemovePermissionOwnerFromGroup(long groupId, long permissionOwnerId) {
228      Response response = new Response();
229
230      UserGroup userGroup = userGroupAdapter.GetById(groupId);
231      if (userGroup == null) {
232        response.Success = false;
233        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
234        return response;
235      }
236      User user = userAdapter.GetById(permissionOwnerId);
237      if (user == null) {
238        response.Success = false;
239        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_DOESNT_EXIST;
240        return response;
241      }
242      foreach (PermissionOwner permissionOwner in userGroup.Members) {
243        if (permissionOwner.Id == permissionOwnerId) {
244          userGroup.Members.Remove(permissionOwner);
245          userGroupAdapter.Update(userGroup);
246          response.Success = true;
247          response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_REMOVED;
248          return response;
249        }
250      }
251      response.Success = false;
252      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_DOESNT_EXIST;
253     
254      return response;
255    }
256
257    #endregion
258  }
259}
Note: See TracBrowser for help on using the repository browser.