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