Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabMembershipProvider.cs @ 3956

Last change on this file since 3956 was 3956, checked in by hmayr, 14 years ago

following changes (#1046):

  • implemented some methods of HeuristicLabMembershipProvider and unit tests
  • tests for ChangePassword and ChangePasswordQuestionAndAnswer do not work at the moment!
File size: 7.0 KB
Line 
1using System;
2using System.Linq;
3using System.Web.Security;
4using Persistence;
5
6namespace Service.Provider {
7  class HeuristicLabMembershipProvider : MembershipProvider {
8    public override string ApplicationName {
9      get {
10        throw new NotImplementedException();
11      }
12      set {
13        throw new NotImplementedException();
14      }
15    }
16
17    public override bool ChangePassword(string username, string oldPassword, string newPassword) {
18      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
19        // check database connection
20        if (db == null) {
21          return false;
22        }
23        try {
24          // try to get user
25          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
26          if (u.ChangePassword(oldPassword, newPassword)) {
27            // save user to database only if needed
28            db.SubmitChanges();
29            return true;
30          } else {
31            return false;
32          }
33        }
34        catch (Exception) {
35          return false;
36        }
37      }
38    }
39
40    public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
41      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
42        // check database connection
43        if (db == null) {
44          return false;
45        }
46        try {
47          // try to get user
48          HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
49          if (u.ChangePasswordQuestionAndAnswer(password, newPasswordQuestion, newPasswordAnswer)) {
50            // save user to database only if needed
51            db.SubmitChanges();
52            return true;
53          } else {
54            return false;
55          }
56        }
57        catch (Exception) {
58          return false;
59        }
60      }
61    }
62
63    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
64      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
65        // check database connection
66        if (db == null) {
67          status = MembershipCreateStatus.ProviderError;
68          return null;
69        }
70        try {
71          // check for duplicate entries
72          if (db.HeuristicLabUsers.Count(x => x.UserName == username) > 0) {
73            status = MembershipCreateStatus.DuplicateUserName;
74            return null;
75          }
76          if (db.HeuristicLabUsers.Count(x => x.Email == email) > 0) {
77            status = MembershipCreateStatus.DuplicateEmail;
78            return null;
79          }
80
81          // create new user
82          HeuristicLabUser u = new HeuristicLabUser(username, email, passwordQuestion, "");
83          u.ChangePassword("INIT", password);
84          u.ChangePasswordQuestionAndAnswer(password, passwordQuestion, passwordAnswer);
85          // save user into database
86          db.HeuristicLabUsers.InsertOnSubmit(u);
87          db.SubmitChanges();
88
89          // success
90          status = MembershipCreateStatus.Success;
91          return u;
92        }
93        catch (Exception) {
94          // error
95          status = MembershipCreateStatus.ProviderError;
96          return null;
97        }
98      }
99    }
100
101    public override bool DeleteUser(string username, bool deleteAllRelatedData) {
102      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
103        // check database connection
104        if (db == null) {
105          return false;
106        }
107        try {
108          // try to get user
109          HeuristicLabUser u =
110            db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == username);
111
112          // optionally delete related data
113          if (deleteAllRelatedData) {
114            db.HeuristicLabUserRole.DeleteAllOnSubmit<HeuristicLabUserRole>(u.HeuristicLabUserRole);
115          }
116         
117          // delete user
118          db.HeuristicLabUsers.DeleteOnSubmit(u);
119          db.SubmitChanges();
120          return true;
121        }
122        catch (Exception) {
123          return false;
124        }
125      }
126    }
127
128    public override bool EnablePasswordReset {
129      get { throw new NotImplementedException(); }
130    }
131
132    public override bool EnablePasswordRetrieval {
133      get { throw new NotImplementedException(); }
134    }
135
136    public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) {
137      throw new NotImplementedException();
138    }
139
140    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) {
141      throw new NotImplementedException();
142    }
143
144    public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) {
145      throw new NotImplementedException();
146    }
147
148    public override int GetNumberOfUsersOnline() {
149      throw new NotImplementedException();
150    }
151
152    public override string GetPassword(string username, string answer) {
153
154      throw new NotImplementedException();
155    }
156
157    public override MembershipUser GetUser(string username, bool userIsOnline) {
158      throw new NotImplementedException();
159    }
160
161    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) {
162      throw new NotImplementedException();
163    }
164
165    public override string GetUserNameByEmail(string email) {
166      throw new NotImplementedException();
167    }
168
169    public override int MaxInvalidPasswordAttempts {
170      get { throw new NotImplementedException(); }
171    }
172
173    public override int MinRequiredNonAlphanumericCharacters {
174      get { throw new NotImplementedException(); }
175    }
176
177    public override int MinRequiredPasswordLength {
178      get { throw new NotImplementedException(); }
179    }
180
181    public override int PasswordAttemptWindow {
182      get { throw new NotImplementedException(); }
183    }
184
185    public override MembershipPasswordFormat PasswordFormat {
186      get { throw new NotImplementedException(); }
187    }
188
189    public override string PasswordStrengthRegularExpression {
190      get { throw new NotImplementedException(); }
191    }
192
193    public override bool RequiresQuestionAndAnswer {
194      get { throw new NotImplementedException(); }
195    }
196
197    public override bool RequiresUniqueEmail {
198      get { throw new NotImplementedException(); }
199    }
200
201    public override string ResetPassword(string username, string answer) {
202      throw new NotImplementedException();
203    }
204
205    public override bool UnlockUser(string userName) {
206      throw new NotImplementedException();
207    }
208
209    public override void UpdateUser(MembershipUser user) {
210      throw new NotImplementedException();
211    }
212
213    public override bool ValidateUser(string username, string password) {
214      throw new NotImplementedException();
215    }
216  }
217}
Note: See TracBrowser for help on using the repository browser.