[1409] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Security.Contracts.Interfaces;
|
---|
| 6 | using HeuristicLab.Security.Contracts.BusinessObjects;
|
---|
| 7 | using HeuristicLab.Security.DataAccess;
|
---|
| 8 | using HeuristicLab.PluginInfrastructure;
|
---|
[1624] | 9 | using HeuristicLab.DataAccess.Interfaces;
|
---|
[1738] | 10 | using System.ServiceModel;
|
---|
[1409] | 11 |
|
---|
| 12 | namespace HeuristicLab.Security.Core {
|
---|
| 13 | public class SecurityManager : ISecurityManager {
|
---|
| 14 |
|
---|
[1724] | 15 | private static ISessionFactory factory = ServiceLocator.GetSessionFactory();
|
---|
[1409] | 16 |
|
---|
[1624] | 17 | private ISession session;
|
---|
[1409] | 18 |
|
---|
[1624] | 19 | /// <summary>
|
---|
| 20 | /// Add new user.
|
---|
| 21 | /// </summary>
|
---|
| 22 | /// <param name="user"></param>
|
---|
| 23 | /// <returns></returns>
|
---|
[1409] | 24 | public User AddNewUser(User user) {
|
---|
[1624] | 25 | try {
|
---|
| 26 | session = factory.GetSessionForCurrentThread();
|
---|
| 27 |
|
---|
| 28 | IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
|
---|
| 29 | if (user != null)
|
---|
| 30 | userAdapter.Update(user);
|
---|
| 31 |
|
---|
| 32 | return user;
|
---|
| 33 | }
|
---|
[1738] | 34 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 35 | finally {
|
---|
| 36 | if (session != null)
|
---|
| 37 | session.EndSession();
|
---|
| 38 | }
|
---|
[1409] | 39 | }
|
---|
| 40 |
|
---|
[1624] | 41 | /// <summary>
|
---|
| 42 | /// Update user.
|
---|
| 43 | /// </summary>
|
---|
| 44 | /// <param name="user"></param>
|
---|
| 45 | /// <returns></returns>
|
---|
[1409] | 46 | public User UpdateUser(User user) {
|
---|
[1624] | 47 | try {
|
---|
| 48 | session = factory.GetSessionForCurrentThread();
|
---|
| 49 | IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
|
---|
| 50 |
|
---|
| 51 | if (user != null)
|
---|
| 52 | userAdapter.Update(user);
|
---|
| 53 | return user;
|
---|
| 54 | }
|
---|
[1738] | 55 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 56 | finally {
|
---|
| 57 | if (session != null)
|
---|
| 58 | session.EndSession();
|
---|
| 59 | }
|
---|
[1409] | 60 | }
|
---|
| 61 |
|
---|
[1624] | 62 | /// <summary>
|
---|
| 63 | /// Remove user.
|
---|
| 64 | /// </summary>
|
---|
| 65 | /// <param name="userId"></param>
|
---|
| 66 | /// <returns></returns>
|
---|
[1409] | 67 | public bool RemoveUser(Guid userId) {
|
---|
[1624] | 68 | try {
|
---|
| 69 | session = factory.GetSessionForCurrentThread();
|
---|
| 70 | IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
|
---|
| 71 | User user = userAdapter.GetById(userId);
|
---|
| 72 |
|
---|
| 73 | if (user != null)
|
---|
| 74 | return userAdapter.Delete(user);
|
---|
| 75 | return false;
|
---|
| 76 | }
|
---|
[1738] | 77 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 78 | finally {
|
---|
| 79 | if (session != null)
|
---|
| 80 | session.EndSession();
|
---|
| 81 | }
|
---|
[1409] | 82 | }
|
---|
| 83 |
|
---|
[1624] | 84 | /// <summary>
|
---|
| 85 | /// Gets all Users.
|
---|
| 86 | /// </summary>
|
---|
| 87 | /// <returns></returns>
|
---|
| 88 | public ICollection<User> GetAllUsers() {
|
---|
| 89 | try {
|
---|
| 90 | session = factory.GetSessionForCurrentThread();
|
---|
| 91 | IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
|
---|
| 92 |
|
---|
| 93 | return userAdapter.GetAll();
|
---|
| 94 | }
|
---|
[1738] | 95 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 96 | finally {
|
---|
| 97 | if (session != null)
|
---|
| 98 | session.EndSession();
|
---|
| 99 | }
|
---|
[1409] | 100 | }
|
---|
| 101 |
|
---|
[1624] | 102 | /// <summary>
|
---|
| 103 | /// Gets user by name.
|
---|
| 104 | /// </summary>
|
---|
| 105 | /// <param name="name"></param>
|
---|
| 106 | /// <returns></returns>
|
---|
| 107 | public User GetUserByName(string name) {
|
---|
| 108 | try {
|
---|
| 109 | session = factory.GetSessionForCurrentThread();
|
---|
| 110 | IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
|
---|
| 111 |
|
---|
| 112 | return userAdapter.GetByName(name);
|
---|
| 113 | }
|
---|
[1738] | 114 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 115 | finally {
|
---|
| 116 | if (session != null)
|
---|
| 117 | session.EndSession();
|
---|
| 118 | }
|
---|
[1409] | 119 | }
|
---|
| 120 |
|
---|
[1624] | 121 | /// <summary>
|
---|
| 122 | /// Add new user group.
|
---|
| 123 | /// </summary>
|
---|
| 124 | /// <param name="group"></param>
|
---|
| 125 | /// <returns></returns>
|
---|
| 126 | public UserGroup AddNewUserGroup(UserGroup userGroup) {
|
---|
| 127 | try {
|
---|
| 128 | session = factory.GetSessionForCurrentThread();
|
---|
| 129 |
|
---|
| 130 | IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
| 131 | if (userGroup != null)
|
---|
| 132 | userGroupAdapter.Update(userGroup);
|
---|
| 133 |
|
---|
| 134 | return userGroup;
|
---|
| 135 | }
|
---|
[1738] | 136 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 137 | finally {
|
---|
| 138 | if (session != null)
|
---|
| 139 | session.EndSession();
|
---|
| 140 | }
|
---|
[1409] | 141 | }
|
---|
| 142 |
|
---|
[1624] | 143 | /// <summary>
|
---|
| 144 | /// Update user group.
|
---|
| 145 | /// </summary>
|
---|
| 146 | /// <param name="group"></param>
|
---|
| 147 | /// <returns></returns>
|
---|
| 148 | public UserGroup UpdateUserGroup(UserGroup userGroup) {
|
---|
| 149 | try {
|
---|
| 150 | session = factory.GetSessionForCurrentThread();
|
---|
| 151 |
|
---|
| 152 | IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
| 153 | if (userGroup != null)
|
---|
| 154 | userGroupAdapter.Update(userGroup);
|
---|
| 155 |
|
---|
| 156 | return userGroup;
|
---|
| 157 | }
|
---|
[1738] | 158 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 159 | finally {
|
---|
| 160 | if (session != null)
|
---|
| 161 | session.EndSession();
|
---|
| 162 | }
|
---|
[1409] | 163 | }
|
---|
| 164 |
|
---|
[1624] | 165 | /// <summary>
|
---|
| 166 | /// Remove user group.
|
---|
| 167 | /// </summary>
|
---|
| 168 | /// <param name="groupId"></param>
|
---|
| 169 | /// <returns></returns>
|
---|
| 170 | public bool RemoveUserGroup(Guid userGroupId) {
|
---|
| 171 | try {
|
---|
| 172 | session = factory.GetSessionForCurrentThread();
|
---|
| 173 |
|
---|
| 174 | IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
| 175 | UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
|
---|
| 176 |
|
---|
| 177 | if (userGroup != null)
|
---|
| 178 | return userGroupAdapter.Delete(userGroup);
|
---|
| 179 | return false;
|
---|
| 180 | }
|
---|
[1738] | 181 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 182 | finally {
|
---|
| 183 | if (session != null)
|
---|
| 184 | session.EndSession();
|
---|
| 185 | }
|
---|
[1409] | 186 | }
|
---|
| 187 |
|
---|
[1624] | 188 | /// <summary>
|
---|
| 189 | /// Gets all UserGroups.
|
---|
| 190 | /// </summary>
|
---|
| 191 | /// <returns></returns>
|
---|
| 192 | public ICollection<UserGroup> GetAllUserGroups() {
|
---|
| 193 | try {
|
---|
| 194 | session = factory.GetSessionForCurrentThread();
|
---|
| 195 | IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
| 196 |
|
---|
| 197 | return userGroupAdapter.GetAll();
|
---|
| 198 | }
|
---|
[1738] | 199 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 200 | finally {
|
---|
| 201 | if (session != null)
|
---|
| 202 | session.EndSession();
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /// <summary>
|
---|
| 207 | /// Gets UserGroup by name.
|
---|
| 208 | /// </summary>
|
---|
| 209 | /// <param name="name"></param>
|
---|
| 210 | /// <returns></returns>
|
---|
| 211 | public UserGroup GetUserGroupByName(string name) {
|
---|
| 212 | try {
|
---|
| 213 | session = factory.GetSessionForCurrentThread();
|
---|
| 214 | IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
| 215 |
|
---|
| 216 | return userGroupAdapter.GetByName(name);
|
---|
| 217 | }
|
---|
[1738] | 218 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 219 | finally {
|
---|
| 220 | if (session != null)
|
---|
| 221 | session.EndSession();
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /// <summary>
|
---|
| 226 | /// Updates a PermissionOwner.
|
---|
| 227 | /// </summary>
|
---|
| 228 | /// <param name="permissionOwner"></param>
|
---|
| 229 | /// <returns></returns>
|
---|
| 230 | public PermissionOwner UpdatePermissionOwner(PermissionOwner permissionOwner) {
|
---|
| 231 | try {
|
---|
| 232 | session = factory.GetSessionForCurrentThread();
|
---|
| 233 |
|
---|
| 234 | IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
|
---|
| 235 | if (permissionOwner != null)
|
---|
| 236 | permOwnerAdapter.Update(permissionOwner);
|
---|
| 237 |
|
---|
| 238 | return permissionOwner;
|
---|
| 239 | }
|
---|
[1738] | 240 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 241 | finally {
|
---|
| 242 | if (session != null)
|
---|
| 243 | session.EndSession();
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /// <summary>
|
---|
| 248 | /// Add permission owner to group.
|
---|
| 249 | /// </summary>
|
---|
| 250 | /// <param name="userGroupId"></param>
|
---|
| 251 | /// <param name="permissionOwnerId"></param>
|
---|
| 252 | /// <returns></returns>
|
---|
| 253 | public bool AddPermissionOwnerToGroup(Guid userGroupId, Guid permissionOwnerId) {
|
---|
| 254 | try {
|
---|
| 255 | session = factory.GetSessionForCurrentThread();
|
---|
| 256 | ITransaction transaction = session.BeginTransaction();
|
---|
| 257 |
|
---|
| 258 | IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
| 259 | UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
|
---|
| 260 |
|
---|
| 261 | IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
|
---|
| 262 | PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
|
---|
| 263 |
|
---|
| 264 | if ((userGroup != null) && (permissionOwner != null)) {
|
---|
| 265 | userGroup.Members.Add(permissionOwner);
|
---|
| 266 | userGroupAdapter.Update(userGroup);
|
---|
| 267 | transaction.Commit();
|
---|
| 268 | return true;
|
---|
| 269 | }
|
---|
| 270 | return false;
|
---|
| 271 | }
|
---|
[1738] | 272 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 273 | finally {
|
---|
| 274 | if (session != null)
|
---|
| 275 | session.EndSession();
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | /// <summary>
|
---|
| 281 | /// Remove permission owner from group.
|
---|
| 282 | /// </summary>
|
---|
| 283 | /// <param name="groupId"></param>
|
---|
| 284 | /// <param name="permissionOwnerId"></param>
|
---|
| 285 | /// <returns></returns>
|
---|
| 286 | public bool RemovePermissionOwnerFromGroup(Guid userGroupId, Guid permissionOwnerId) {
|
---|
| 287 | try {
|
---|
| 288 | session = factory.GetSessionForCurrentThread();
|
---|
| 289 | ITransaction transaction = session.BeginTransaction();
|
---|
| 290 |
|
---|
| 291 | IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
| 292 | UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
|
---|
| 293 |
|
---|
| 294 | IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
|
---|
| 295 | PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
|
---|
| 296 |
|
---|
| 297 | if ((userGroup != null) && (permissionOwner != null)) {
|
---|
| 298 | userGroup.Members.Add(permissionOwner);
|
---|
| 299 | userGroupAdapter.Delete(userGroup);
|
---|
| 300 | transaction.Commit();
|
---|
| 301 | return true;
|
---|
| 302 | }
|
---|
| 303 | return false;
|
---|
| 304 | }
|
---|
[1738] | 305 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 306 | finally {
|
---|
| 307 | if (session != null)
|
---|
| 308 | session.EndSession();
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /// <summary>
|
---|
| 313 | /// Grant permission.
|
---|
| 314 | /// </summary>
|
---|
| 315 | /// <param name="permissionOwnerId"></param>
|
---|
| 316 | /// <param name="permissionId"></param>
|
---|
| 317 | /// <param name="entityId"></param>
|
---|
| 318 | /// <returns></returns>
|
---|
[1409] | 319 | public bool GrantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
|
---|
[1624] | 320 | try {
|
---|
| 321 | session = factory.GetSessionForCurrentThread();
|
---|
| 322 | IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
|
---|
| 323 |
|
---|
| 324 | return permissionAdapter.grantPermission(permissionOwnerId, permissionId, entityId);
|
---|
| 325 | }
|
---|
[1738] | 326 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 327 | finally {
|
---|
| 328 | if (session != null)
|
---|
| 329 | session.EndSession();
|
---|
| 330 | }
|
---|
[1409] | 331 | }
|
---|
| 332 |
|
---|
[1624] | 333 | /// <summary>
|
---|
| 334 | /// Gets Permission by ID.
|
---|
| 335 | /// </summary>
|
---|
| 336 | /// <param name="permissionId"></param>
|
---|
| 337 | /// <returns></returns>
|
---|
| 338 | public Permission GetPermissionById(Guid permissionId) {
|
---|
| 339 | try {
|
---|
| 340 | session = factory.GetSessionForCurrentThread();
|
---|
| 341 |
|
---|
| 342 | IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
|
---|
| 343 | return permissionAdapter.GetById(permissionId);
|
---|
| 344 | }
|
---|
[1738] | 345 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 346 | finally {
|
---|
| 347 | if (session != null)
|
---|
| 348 | session.EndSession();
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /// <summary>
|
---|
| 353 | /// Revoke permission.
|
---|
| 354 | /// </summary>
|
---|
| 355 | /// <param name="permissionOwnerId"></param>
|
---|
| 356 | /// <param name="permissionId"></param>
|
---|
| 357 | /// <param name="entityId"></param>
|
---|
| 358 | /// <returns></returns>
|
---|
[1409] | 359 | public bool RevokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
|
---|
[1624] | 360 | try {
|
---|
| 361 | session = factory.GetSessionForCurrentThread();
|
---|
| 362 | IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
|
---|
| 363 |
|
---|
| 364 | return permissionAdapter.revokePermission(permissionOwnerId, permissionId, entityId);
|
---|
| 365 | }
|
---|
[1738] | 366 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1624] | 367 | finally {
|
---|
| 368 | if (session != null)
|
---|
| 369 | session.EndSession();
|
---|
| 370 | }
|
---|
[1409] | 371 | }
|
---|
[1729] | 372 |
|
---|
| 373 | public Permission AddPermission(Permission permission) {
|
---|
| 374 | try {
|
---|
| 375 | session = factory.GetSessionForCurrentThread();
|
---|
| 376 | IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
|
---|
| 377 |
|
---|
| 378 | if (permission != null) {
|
---|
| 379 | permissionAdapter.Update(permission);
|
---|
| 380 |
|
---|
| 381 | return permission;
|
---|
| 382 | } else
|
---|
| 383 | return null;
|
---|
| 384 | }
|
---|
[1738] | 385 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1729] | 386 | finally {
|
---|
| 387 | if (session != null)
|
---|
| 388 | session.EndSession();
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | public bool RemovePermission(Guid permissionId) {
|
---|
| 393 | try {
|
---|
| 394 | session = factory.GetSessionForCurrentThread();
|
---|
| 395 | IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
|
---|
| 396 |
|
---|
| 397 | Permission permission = permissionAdapter.GetById(permissionId);
|
---|
| 398 | if (permission != null)
|
---|
| 399 | return permissionAdapter.Delete(permission);
|
---|
| 400 | else
|
---|
| 401 | return false;
|
---|
| 402 | }
|
---|
[1738] | 403 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1729] | 404 | finally {
|
---|
| 405 | if (session != null)
|
---|
| 406 | session.EndSession();
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | public Permission UpdatePermission(Permission permission) {
|
---|
| 411 | try {
|
---|
| 412 | session = factory.GetSessionForCurrentThread();
|
---|
| 413 | IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
|
---|
| 414 |
|
---|
| 415 | if(permission != null) {
|
---|
| 416 | permissionAdapter.Update(permission);
|
---|
| 417 | return permission;
|
---|
| 418 | }
|
---|
| 419 | else
|
---|
| 420 | return null;
|
---|
| 421 | }
|
---|
[1738] | 422 | catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
|
---|
[1729] | 423 | finally {
|
---|
| 424 | if (session != null)
|
---|
| 425 | session.EndSession();
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
[1409] | 428 | }
|
---|
| 429 | }
|
---|