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

Last change on this file since 995 was 995, checked in by svonolfe, 12 years ago

Refactored DAL, Improved Caching (#372)

File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.Interfaces;
6using HeuristicLab.Hive.Contracts.BusinessObjects;
7using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
8using System.Resources;
9using System.Reflection;
10using HeuristicLab.Hive.Contracts;
11
12namespace HeuristicLab.Hive.Server.Core {
13  class UserRoleManager: IUserRoleManager {
14
15    IUserAdapter userAdapter;
16    IUserGroupAdapter userGroupAdapter;
17    IPermissionOwnerAdapter permissionOwnerAdapter;
18
19    public UserRoleManager() {
20      userAdapter = ServiceLocator.GetUserAdapter();
21      userGroupAdapter = ServiceLocator.GetUserGroupAdapter();
22    }
23
24    #region IUserRoleManager Members
25
26    public ResponseList<User> GetAllUsers() {
27      ResponseList<User> response = new ResponseList<User>();
28
29      List<User> allUsers = new List<User>(userAdapter.GetAll());
30      response.List = allUsers;
31      response.Success = true;
32      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERS;
33
34      return response;
35    }
36
37    public ResponseObject<User> AddNewUser(User user) {
38      ResponseObject<User> response = new ResponseObject<User>();
39
40      if (user.Id != 0) {
41        response.Success = false;
42        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
43        return response;
44      }
45      if (userAdapter.GetByName(user.Name) != null) {
46        response.Success = false;
47        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERNAME_EXISTS_ALLREADY;
48        return response;
49      }
50
51      userAdapter.Update(user);
52      response.Obj = user;
53      response.Success = true;
54      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_SUCCESSFULLY_ADDED;
55     
56      return response;
57    }
58
59    public ResponseList<UserGroup> GetAllUserGroups() {
60      ResponseList<UserGroup> response = new ResponseList<UserGroup>();
61
62      response.List = new List<UserGroup>(userGroupAdapter.GetAll());
63      response.Success = true;
64      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERGROUPS;
65
66      return response;
67    }
68
69    public Response RemoveUser(long userId) {
70      Response response = new Response();
71      User user = userAdapter.GetById(userId);
72      if (user == null) {
73        response.Success = false;
74        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_DOESNT_EXIST;
75        return response;
76      }
77      userAdapter.Delete(user);
78      response.Success = true;
79      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_REMOVED;
80                         
81      return response;
82    }
83
84    public ResponseObject<UserGroup> AddNewUserGroup(UserGroup userGroup) {
85      ResponseObject<UserGroup> response = new ResponseObject<UserGroup>();
86     
87      if (userGroup.Id != 0) {
88        response.Success = false;
89        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
90        return response;
91      }
92
93      userGroupAdapter.Update(userGroup);
94      response.Obj = userGroup;
95      response.Success = false;
96      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
97
98      return response;
99    }
100
101    public Response RemoveUserGroup(long groupId) {
102      Response response = new Response();
103
104      UserGroup userGroupFromDb = userGroupAdapter.GetById(groupId);
105      if (userGroupFromDb == null) {
106        response.Success = false;
107        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
108        return response;
109      }
110      userGroupAdapter.Delete(userGroupFromDb);
111      response.Success = false;
112      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
113
114      return response;
115    }
116
117    public Response AddUserToGroup(long groupId, long userId) {
118      Response response = new Response();
119
120      User user = userAdapter.GetById(userId);
121      if (user == null) {
122        response.Success = false;
123        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_DOESNT_EXIST;
124        return response;
125      }
126
127      UserGroup userGroup = userGroupAdapter.GetById(groupId);
128      if (userGroup == null) {
129        response.Success = false;
130        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
131        return response;
132      }
133      userGroup.Members.Add(user);
134      userGroupAdapter.Update(userGroup);
135
136      response.Success = true;
137      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_ADDED;
138
139      return response;
140    }
141
142    public Response AddUserGroupToGroup(long groupId, long groupToAddId) {
143      Response response = new Response();
144
145      UserGroup userGroup = userGroupAdapter.GetById(groupId);
146      UserGroup userGroupToAdd = userGroupAdapter.GetById(groupToAddId);
147
148      if (userGroup == null || userGroupToAdd == null) {
149        response.Success = false;
150        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
151        return response;
152      }
153      userGroup.Members.Add(userGroupToAdd);
154      response.Success = true;
155      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED_TO_USERGROUP;
156
157      return response;
158    }
159
160    public Response RemovePermissionOwnerFromGroup(long groupId, long permissionOwnerId) {
161      Response response = new Response();
162
163      UserGroup userGroup = userGroupAdapter.GetById(groupId);
164      if (userGroup == null) {
165        response.Success = false;
166        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
167        return response;
168      }
169      User user = userAdapter.GetById(permissionOwnerId);
170      if (user == null) {
171        response.Success = false;
172        response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_DOESNT_EXIST;
173        return response;
174      }
175      foreach (PermissionOwner permissionOwner in userGroup.Members) {
176        if (permissionOwner.Id == permissionOwnerId) {
177          userGroup.Members.Remove(permissionOwner);
178          userGroupAdapter.Update(userGroup);
179          response.Success = true;
180          response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_REMOVED;
181          return response;
182        }
183      }
184      response.Success = false;
185      response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_DOESNT_EXIST;
186     
187      return response;
188    }
189
190    #endregion
191  }
192}
Note: See TracBrowser for help on using the repository browser.