Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1783 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
RevLine 
[1121]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;
[800]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
[902]28using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
29using System.Resources;
30using System.Reflection;
31using HeuristicLab.Hive.Contracts;
[800]32
33namespace HeuristicLab.Hive.Server.Core {
34  class UserRoleManager: IUserRoleManager {
[820]35
[902]36    IUserAdapter userAdapter;
[934]37    IUserGroupAdapter userGroupAdapter;
[945]38    IPermissionOwnerAdapter permissionOwnerAdapter;
[902]39
[820]40    public UserRoleManager() {
[902]41      userAdapter = ServiceLocator.GetUserAdapter();
[934]42      userGroupAdapter = ServiceLocator.GetUserGroupAdapter();
[820]43    }
44
[800]45    #region IUserRoleManager Members
46
[1121]47    /// <summary>
48    /// returns all users from the database
49    /// </summary>
50    /// <returns></returns>
[902]51    public ResponseList<User> GetAllUsers() {
[907]52      ResponseList<User> response = new ResponseList<User>();
53
[995]54      List<User> allUsers = new List<User>(userAdapter.GetAll());
[907]55      response.List = allUsers;
56      response.Success = true;
57      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERS;
58
59      return response;
[800]60    }
61
[1121]62    /// <summary>
63    /// Adds a new user to the database
64    /// </summary>
65    /// <param name="user"></param>
66    /// <returns></returns>
[952]67    public ResponseObject<User> AddNewUser(User user) {
68      ResponseObject<User> response = new ResponseObject<User>();
[907]69
[995]70      if (user.Id != 0) {
[907]71        response.Success = false;
[937]72        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
[907]73        return response;
74      }
[995]75      if (userAdapter.GetByName(user.Name) != null) {
[969]76        response.Success = false;
77        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERNAME_EXISTS_ALLREADY;
78        return response;
79      }
80
[995]81      userAdapter.Update(user);
[952]82      response.Obj = user;
[907]83      response.Success = true;
84      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_SUCCESSFULLY_ADDED;
85     
86      return response;
[842]87    }
88
[1121]89    /// <summary>
90    /// returns all usergroups from the database
91    /// </summary>
92    /// <returns></returns>
[902]93    public ResponseList<UserGroup> GetAllUserGroups() {
[907]94      ResponseList<UserGroup> response = new ResponseList<UserGroup>();
95
[995]96      response.List = new List<UserGroup>(userGroupAdapter.GetAll());
[934]97      response.Success = true;
98      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERGROUPS;
99
[907]100      return response;
[800]101    }
102
[1121]103    /// <summary>
104    /// Removes a user from the database
105    /// </summary>
106    /// <param name="userId"></param>
107    /// <returns></returns>
[902]108    public Response RemoveUser(long userId) {
[907]109      Response response = new Response();
[995]110      User user = userAdapter.GetById(userId);
[907]111      if (user == null) {
112        response.Success = false;
113        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_DOESNT_EXIST;
114        return response;
115      }
[995]116      userAdapter.Delete(user);
[907]117      response.Success = true;
118      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_REMOVED;
[929]119                         
[907]120      return response;
[902]121    }
122
[1121]123    /// <summary>
124    /// Adds a new usergroup to the database
125    /// </summary>
126    /// <param name="userGroup"></param>
127    /// <returns></returns>
[952]128    public ResponseObject<UserGroup> AddNewUserGroup(UserGroup userGroup) {
129      ResponseObject<UserGroup> response = new ResponseObject<UserGroup>();
[937]130     
[995]131      if (userGroup.Id != 0) {
[934]132        response.Success = false;
[937]133        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
[934]134        return response;
135      }
[969]136
[995]137      userGroupAdapter.Update(userGroup);
[952]138      response.Obj = userGroup;
[934]139      response.Success = false;
140      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
141
142      return response;
[902]143    }
144
[1121]145    /// <summary>
146    /// Removes a user group from the database
147    /// </summary>
148    /// <param name="groupId"></param>
149    /// <returns></returns>
[902]150    public Response RemoveUserGroup(long groupId) {
[934]151      Response response = new Response();
152
[995]153      UserGroup userGroupFromDb = userGroupAdapter.GetById(groupId);
[934]154      if (userGroupFromDb == null) {
155        response.Success = false;
156        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
157        return response;
158      }
[995]159      userGroupAdapter.Delete(userGroupFromDb);
[934]160      response.Success = false;
161      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
162
163      return response;
[902]164    }
165
[1121]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>
[945]172    public Response AddUserToGroup(long groupId, long userId) {
[934]173      Response response = new Response();
[937]174
[995]175      User user = userAdapter.GetById(userId);
[945]176      if (user == null) {
[937]177        response.Success = false;
[945]178        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_DOESNT_EXIST;
[937]179        return response;
180      }
181
[995]182      UserGroup userGroup = userGroupAdapter.GetById(groupId);
[934]183      if (userGroup == null) {
184        response.Success = false;
185        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
186        return response;
187      }
[945]188      userGroup.Members.Add(user);
[995]189      userGroupAdapter.Update(userGroup);
[934]190
191      response.Success = true;
192      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_ADDED;
193
194      return response;
[902]195    }
196
[1121]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>
[945]203    public Response AddUserGroupToGroup(long groupId, long groupToAddId) {
204      Response response = new Response();
205
[995]206      UserGroup userGroup = userGroupAdapter.GetById(groupId);
207      UserGroup userGroupToAdd = userGroupAdapter.GetById(groupToAddId);
[947]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
[945]218      return response;
219    }
220
[1121]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>
[934]227    public Response RemovePermissionOwnerFromGroup(long groupId, long permissionOwnerId) {
228      Response response = new Response();
229
[995]230      UserGroup userGroup = userGroupAdapter.GetById(groupId);
[934]231      if (userGroup == null) {
232        response.Success = false;
233        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
234        return response;
235      }
[995]236      User user = userAdapter.GetById(permissionOwnerId);
[934]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) {
[995]243        if (permissionOwner.Id == permissionOwnerId) {
[934]244          userGroup.Members.Remove(permissionOwner);
[995]245          userGroupAdapter.Update(userGroup);
[934]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;
[902]255    }
256
[800]257    #endregion
258  }
259}
Note: See TracBrowser for help on using the repository browser.