Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/26/10 10:18:57 (14 years ago)
Author:
hmayr
Message:

following changes (#1046):

  • implemented some methods of HeuristicLabMembershipProvider and unit tests
  • tests for ChangePassword and ChangePasswordQuestionAndAnswer do not work at the moment!
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabMembershipProvider.cs

    r3948 r3956  
    1616
    1717    public override bool ChangePassword(string username, string oldPassword, string newPassword) {
    18       DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
    19       if (db == null) {
    20         return false;
    21       }
    22       try {
    23         HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
    24         if (u.ChangePassword(oldPassword, 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);
    25119          db.SubmitChanges();
    26120          return true;
    27         } else {
    28           return false;
    29         }
    30       }
    31       catch (Exception) {
    32         return false;
    33       }
    34     }
    35 
    36     public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
    37       DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext();
    38       if (db == null) {
    39         return false;
    40       }
    41       try {
    42         HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
    43         if (u.ChangePasswordQuestionAndAnswer(password, newPasswordQuestion, newPasswordAnswer)) {
    44           db.SubmitChanges();
    45           return true;
    46         } else {
    47           return false;
    48         }
    49       }
    50       catch (Exception) {
    51         return false;
    52       }
    53     }
    54 
    55     public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
    56       throw new NotImplementedException();
    57     }
    58 
    59     public override bool DeleteUser(string username, bool deleteAllRelatedData) {
    60       throw new NotImplementedException();
     121        }
     122        catch (Exception) {
     123          return false;
     124        }
     125      }
    61126    }
    62127
     
    86151
    87152    public override string GetPassword(string username, string answer) {
     153
    88154      throw new NotImplementedException();
    89155    }
Note: See TracChangeset for help on using the changeset viewer.