Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UserManagement/HeuristicLab.Services.Authentication/Convert.cs @ 5815

Last change on this file since 5815 was 5257, checked in by mjesner, 14 years ago

#1196

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
21using HeuristicLab.Services.Authentication.DataAccess;
22using HeuristicLab.Services.Authentication.DataTransfer;
23
24namespace HeuristicLab.Services.Authentication {
25  public class Convert {
26
27    #region User
28
29    public static User ToDto(aspnet_User sourceUser, aspnet_Membership sourceMembership) {
30      if (sourceUser == null || sourceMembership == null) return null;
31      return new User() {
32        ApplicationId = sourceUser.ApplicationId,
33        Id = sourceUser.UserId,
34        Name = sourceUser.UserName,
35        LastActivityDate = sourceUser.LastActivityDate,
36        Email = sourceMembership.Email,
37        IsApproved = sourceMembership.IsApproved,
38        IsLookedOut = sourceMembership.IsApproved,
39        CreateDate = sourceMembership.CreateDate,
40        LastLoginDate = sourceMembership.LastLoginDate,
41        LastPasswordChangeDate = sourceMembership.LastPasswordChangedDate,
42        LastLockoutDate = sourceMembership.LastLockoutDate,
43        Description = sourceMembership.Comment
44      };
45    }
46
47    public static void ToEntity(User sourceUser, aspnet_User targetUser, aspnet_Membership targetMembership) {
48      if ((sourceUser != null) && (targetUser != null) && (targetMembership != null)) {
49        targetUser.ApplicationId = sourceUser.ApplicationId;
50        targetUser.UserId = sourceUser.Id;
51        targetUser.UserName = sourceUser.Name;
52        targetUser.LoweredUserName = sourceUser.Name;
53        targetUser.IsAnonymous = false;
54        targetUser.LastActivityDate = sourceUser.LastActivityDate;
55        targetMembership.UserId = sourceUser.Id;
56        targetMembership.ApplicationId = sourceUser.ApplicationId;
57        targetMembership.PasswordFormat = 1;
58        targetMembership.Email = sourceUser.Email;
59        targetMembership.IsApproved = sourceUser.IsApproved;
60        targetMembership.IsLockedOut = sourceUser.IsLookedOut;
61        targetMembership.CreateDate = sourceUser.CreateDate;
62        targetMembership.LastLoginDate = sourceUser.LastLoginDate;
63        targetMembership.LastPasswordChangedDate = sourceUser.LastPasswordChangeDate;
64        targetMembership.LastLockoutDate = sourceUser.LastLockoutDate;
65        targetMembership.FailedPasswordAttemptCount = 0;
66        targetMembership.FailedPasswordAttemptWindowStart = new System.DateTime(1900, 01, 01);
67        targetMembership.FailedPasswordAnswerAttemptCount = 0;
68        targetMembership.FailedPasswordAnswerAttemptWindowStart = new System.DateTime(1900, 01, 01);
69        targetMembership.Comment = sourceUser.Description;
70      }
71    }
72
73    public static void ToEntity(User source, out aspnet_User userTarget, out aspnet_Membership membershipTarget) {
74      userTarget = new aspnet_User();
75      membershipTarget = new aspnet_Membership();
76
77      if ((source != null)) {
78        userTarget.ApplicationId = source.ApplicationId;
79        userTarget.UserId = source.Id;
80        userTarget.UserName = source.Name;
81        userTarget.LoweredUserName = source.Name;
82        userTarget.IsAnonymous = false;
83        userTarget.LastActivityDate = source.LastActivityDate;
84        membershipTarget.UserId = source.Id;
85        membershipTarget.ApplicationId = source.ApplicationId;
86        membershipTarget.PasswordFormat = 1;
87        membershipTarget.Email = source.Email;
88        membershipTarget.IsApproved = source.IsApproved;
89        membershipTarget.IsLockedOut = source.IsLookedOut;
90        membershipTarget.CreateDate = source.CreateDate;
91        membershipTarget.LastLoginDate = source.LastLoginDate;
92        membershipTarget.LastPasswordChangedDate = source.LastPasswordChangeDate;
93        membershipTarget.LastLockoutDate = source.LastLockoutDate;
94        membershipTarget.FailedPasswordAttemptCount = 0;
95        membershipTarget.FailedPasswordAttemptWindowStart = new System.DateTime(1900, 01, 01);
96        membershipTarget.FailedPasswordAnswerAttemptCount = 0;
97        membershipTarget.FailedPasswordAnswerAttemptWindowStart = new System.DateTime(1900, 01, 01);
98        membershipTarget.Comment = source.Description;
99      }
100    }
101
102    #endregion
103
104    #region Application
105
106    public static Application ToDto(aspnet_Application sourceApplication) {
107      if (sourceApplication == null) return null;
108      return new Application() {
109        Id = sourceApplication.ApplicationId,
110        Name = sourceApplication.ApplicationName,
111        Description = sourceApplication.Description
112      };
113    }
114
115    public static void ToEntity(Application sourceApplication, aspnet_Application targetApplication) {
116      if ((sourceApplication != null) && (targetApplication != null)) {
117        targetApplication.ApplicationId = sourceApplication.Id;
118        targetApplication.ApplicationName = sourceApplication.Name;
119        targetApplication.LoweredApplicationName = sourceApplication.Name.ToLower();
120        targetApplication.Description = sourceApplication.Description;
121      }
122    }
123
124    public static aspnet_Application ToEntity(Application sourceApplication) {
125      if (sourceApplication == null) return null;
126      return new aspnet_Application() {
127        ApplicationId = sourceApplication.Id,
128        ApplicationName = sourceApplication.Name,
129        LoweredApplicationName = sourceApplication.Name.ToLower(),
130        Description = sourceApplication.Description
131      };
132    }
133
134    #endregion
135
136    #region Role
137
138    public static Role ToDto(aspnet_Role sourceRole) {
139      if (sourceRole == null) return null;
140      return new Role() {
141        ApplicationId = sourceRole.ApplicationId,
142        Id = sourceRole.RoleId,
143        Name = sourceRole.RoleName,
144        Description = sourceRole.Description,
145      };
146    }
147
148    public static void ToEntity(Role sourceRole, aspnet_Role targetRole) {
149      if ((sourceRole != null) && (targetRole != null)) {
150        targetRole.ApplicationId = sourceRole.ApplicationId;
151        targetRole.RoleId = sourceRole.Id;
152        targetRole.RoleName = sourceRole.Name;
153        targetRole.LoweredRoleName = sourceRole.Name.ToLower();
154        targetRole.Description = sourceRole.Description;
155      }
156    }
157
158    public static aspnet_Role ToEntity(Role sourceRole) {
159      if (sourceRole == null) return null;
160      return new aspnet_Role() {
161        ApplicationId = sourceRole.ApplicationId,
162        RoleId = sourceRole.Id,
163        RoleName = sourceRole.Name,
164        LoweredRoleName = sourceRole.Name.ToLower(),
165        Description = sourceRole.Description,
166      };
167    }
168
169    #endregion
170  }
171}
Note: See TracBrowser for help on using the repository browser.