1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
6 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
7 | using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
|
---|
8 | using System.Resources;
|
---|
9 | using System.Reflection;
|
---|
10 | using HeuristicLab.Hive.Contracts;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Hive.Server.Core {
|
---|
13 | class UserRoleManager: IUserRoleManager {
|
---|
14 |
|
---|
15 | IUserAdapter userAdapter;
|
---|
16 | IUserGroupAdapter userGroupAdapter;
|
---|
17 |
|
---|
18 | public UserRoleManager() {
|
---|
19 | userAdapter = ServiceLocator.GetUserAdapter();
|
---|
20 | userGroupAdapter = ServiceLocator.GetUserGroupAdapter();
|
---|
21 | }
|
---|
22 |
|
---|
23 | #region IUserRoleManager Members
|
---|
24 |
|
---|
25 | public ResponseList<User> GetAllUsers() {
|
---|
26 | ResponseList<User> response = new ResponseList<User>();
|
---|
27 |
|
---|
28 | List<User> allUsers = new List<User>(userAdapter.GetAllUsers());
|
---|
29 | response.List = allUsers;
|
---|
30 | response.Success = true;
|
---|
31 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERS;
|
---|
32 |
|
---|
33 | return response;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public Response AddNewUser(User user) {
|
---|
37 | Response response = new Response();
|
---|
38 |
|
---|
39 | if (user.PermissionOwnerId != null) {
|
---|
40 | response.Success = false;
|
---|
41 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
|
---|
42 | return response;
|
---|
43 | }
|
---|
44 | userAdapter.UpdateUser(user);
|
---|
45 | response.Success = true;
|
---|
46 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_SUCCESSFULLY_ADDED;
|
---|
47 |
|
---|
48 | return response;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public ResponseList<UserGroup> GetAllUserGroups() {
|
---|
52 | ResponseList<UserGroup> response = new ResponseList<UserGroup>();
|
---|
53 |
|
---|
54 | response.List = new List<UserGroup>(userGroupAdapter.GetAllUserGroups());
|
---|
55 | response.Success = true;
|
---|
56 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_GET_ALL_USERGROUPS;
|
---|
57 |
|
---|
58 | return response;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public Response RemoveUser(long userId) {
|
---|
62 | Response response = new Response();
|
---|
63 | User user = userAdapter.GetUserById(userId);
|
---|
64 | if (user == null) {
|
---|
65 | response.Success = false;
|
---|
66 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_DOESNT_EXIST;
|
---|
67 | return response;
|
---|
68 | }
|
---|
69 | userAdapter.DeleteUser(user);
|
---|
70 | response.Success = true;
|
---|
71 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USER_REMOVED;
|
---|
72 |
|
---|
73 | return response;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public Response AddNewUserGroup(UserGroup userGroup) {
|
---|
77 | Response response = new Response();
|
---|
78 |
|
---|
79 | if (userGroup.PermissionOwnerId != null) {
|
---|
80 | response.Success = false;
|
---|
81 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
|
---|
82 | return response;
|
---|
83 | }
|
---|
84 | userGroupAdapter.UpdateUserGroup(userGroup);
|
---|
85 | response.Success = false;
|
---|
86 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
|
---|
87 |
|
---|
88 | return response;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public Response RemoveUserGroup(long groupId) {
|
---|
92 | Response response = new Response();
|
---|
93 |
|
---|
94 | UserGroup userGroupFromDb = userGroupAdapter.GetUserGroupById(groupId);
|
---|
95 | if (userGroupFromDb == null) {
|
---|
96 | response.Success = false;
|
---|
97 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
|
---|
98 | return response;
|
---|
99 | }
|
---|
100 | userGroupAdapter.DeleteUserGroup(userGroupFromDb);
|
---|
101 | response.Success = false;
|
---|
102 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_ADDED;
|
---|
103 |
|
---|
104 | return response;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public Response AddPermissionOwnerToGroup(long groupId, PermissionOwner permissionOwner) {
|
---|
108 | Response response = new Response();
|
---|
109 |
|
---|
110 | if (permissionOwner.PermissionOwnerId != null) {
|
---|
111 | response.Success = false;
|
---|
112 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_ID_MUST_NOT_BE_SET;
|
---|
113 | return response;
|
---|
114 | }
|
---|
115 |
|
---|
116 | UserGroup userGroup = userGroupAdapter.GetUserGroupById(groupId);
|
---|
117 | if (userGroup == null) {
|
---|
118 | response.Success = false;
|
---|
119 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
|
---|
120 | return response;
|
---|
121 | }
|
---|
122 | userGroup.Members.Add(permissionOwner);
|
---|
123 | userGroupAdapter.UpdateUserGroup(userGroup);
|
---|
124 |
|
---|
125 | response.Success = true;
|
---|
126 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_ADDED;
|
---|
127 |
|
---|
128 | return response;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public Response RemovePermissionOwnerFromGroup(long groupId, long permissionOwnerId) {
|
---|
132 | Response response = new Response();
|
---|
133 |
|
---|
134 | UserGroup userGroup = userGroupAdapter.GetUserGroupById(groupId);
|
---|
135 | if (userGroup == null) {
|
---|
136 | response.Success = false;
|
---|
137 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_USERGROUP_DOESNT_EXIST;
|
---|
138 | return response;
|
---|
139 | }
|
---|
140 | User user = userAdapter.GetUserById(permissionOwnerId);
|
---|
141 | if (user == null) {
|
---|
142 | response.Success = false;
|
---|
143 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_DOESNT_EXIST;
|
---|
144 | return response;
|
---|
145 | }
|
---|
146 | foreach (PermissionOwner permissionOwner in userGroup.Members) {
|
---|
147 | if (permissionOwner.PermissionOwnerId == permissionOwnerId) {
|
---|
148 | userGroup.Members.Remove(permissionOwner);
|
---|
149 | userGroupAdapter.UpdateUserGroup(userGroup);
|
---|
150 | response.Success = true;
|
---|
151 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_REMOVED;
|
---|
152 | return response;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | response.Success = false;
|
---|
156 | response.StatusMessage = ApplicationConstants.RESPONSE_USERROLE_PERMISSIONOWNER_DOESNT_EXIST;
|
---|
157 |
|
---|
158 | return response;
|
---|
159 | }
|
---|
160 |
|
---|
161 | #endregion
|
---|
162 | }
|
---|
163 | }
|
---|