Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/UserAdapter.cs @ 939

Last change on this file since 939 was 939, checked in by svonolfe, 15 years ago

Fixed referential integrity between user and user group (#372)

File size: 4.8 KB
Line 
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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26
27using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
28using HeuristicLab.Hive.Contracts.BusinessObjects;
29using System.Runtime.CompilerServices;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
32  class UserAdapter : DataAdapterBase, IUserAdapter {
33    private dsHiveServerTableAdapters.HiveUserTableAdapter adapter =
34        new dsHiveServerTableAdapters.HiveUserTableAdapter();
35
36    private dsHiveServer.HiveUserDataTable data =
37        new dsHiveServer.HiveUserDataTable();
38
39    private IPermissionOwnerAdapter permOwnerAdapter =
40       ServiceLocator.GetPermissionOwnerAdapter();
41
42    private IUserGroupAdapter userGroupAdapter = null;
43
44    public UserAdapter() {
45      adapter.Fill(data);
46    }
47
48    protected override void Update() {
49      this.adapter.Update(this.data);
50    }
51
52    private User Convert(dsHiveServer.HiveUserRow row,
53      User user) {
54      if (row != null && user != null) {
55        /*Parent - PermissionOwner*/
56        user.PermissionOwnerId = row.PermissionOwnerId;
57        permOwnerAdapter.GetPermissionOwnerById(user);
58
59        /*User*/
60        if (!row.IsPasswordNull())
61          user.Password = row.Password;
62        else
63          user.Password = String.Empty;
64
65        return user;
66      } else
67        return null;
68    }
69
70    private dsHiveServer.HiveUserRow Convert(User user,
71      dsHiveServer.HiveUserRow row) {
72      if (user != null && row != null) {
73        row.Password = user.Password;
74
75        return row;
76      } else
77        return null;     
78    }
79
80    #region IUserAdapter Members
81
82    [MethodImpl(MethodImplOptions.Synchronized)]
83    public void UpdateUser(User user) {
84      if (user != null) {
85        permOwnerAdapter.UpdatePermissionOwner(user);
86
87        dsHiveServer.HiveUserRow row =
88          data.FindByPermissionOwnerId(user.PermissionOwnerId);
89        if (row == null) {
90          row = data.NewHiveUserRow();
91          row.PermissionOwnerId = user.PermissionOwnerId;
92          data.AddHiveUserRow(row);
93        }
94
95        Convert(user, row);
96      }
97    }
98
99    public User GetUserById(long userId) {
100      User user = new User();
101
102      dsHiveServer.HiveUserRow row =
103          data.FindByPermissionOwnerId(userId);
104
105      if(row != null) {
106        Convert(row, user);
107
108        return user;
109      } else {
110        return null;
111      }
112    }
113
114    public User GetUserByName(String name) {
115      User user = new User();
116
117      PermissionOwner permOwner =
118        permOwnerAdapter.GetPermissionOwnerByName(name);
119
120      if (permOwner != null) {
121        dsHiveServer.HiveUserRow row =
122          data.FindByPermissionOwnerId(permOwner.PermissionOwnerId);
123
124        if (row != null) {
125          Convert(row, user);
126
127          return user;
128        }
129      }
130
131      return null;
132    }
133
134    public ICollection<User> GetAllUsers() {
135      ICollection<User> allUsers =
136        new List<User>();
137
138      foreach (dsHiveServer.HiveUserRow row in data) {
139        User user = new User();
140        Convert(row, user);
141        allUsers.Add(user);
142      }
143
144      return allUsers;
145    }
146
147    [MethodImpl(MethodImplOptions.Synchronized)]
148    public bool DeleteUser(User user) {
149      if (user != null) {
150        dsHiveServer.HiveUserRow row =
151          data.FindByPermissionOwnerId(user.PermissionOwnerId);
152
153        if (row != null) {
154          if (userGroupAdapter == null)
155            userGroupAdapter =
156              ServiceLocator.GetUserGroupAdapter();
157
158          ICollection<UserGroup> userGroups =
159            userGroupAdapter.MemberOf(user);
160
161          foreach (UserGroup group in userGroups) {
162            group.Members.Remove(user);
163            userGroupAdapter.UpdateUserGroup(group);
164          }
165
166          data.RemoveHiveUserRow(row);
167
168          return permOwnerAdapter.DeletePermissionOwner(user);
169        }
170      }
171
172      return false;
173    }
174
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.