Esempio n. 1
0
 public static Guid CreateUser(string username, string password, string firstname, string lastname, string email, string psychlotron)
 {
     if (username.ToLower() == VISITOR_USERNAME) {
         var newUser = User.CreateVisitor();
         using (var dc = new PsyDc()) {
             dc.Users.InsertOnSubmit(newUser);
             dc.SubmitChanges();
             if (newUser.UserId.IsValidId()) {
                 newUser.Username = "******".Fmt(newUser.Username, newUser.SerialNumber);
                 dc.SubmitChanges_ResolveAll();
                 return newUser.UserId;
             }
         }
     }
     else if (ValidateUserName(username) && ValidatePassword(password) && ValidateEmail(email) && ValidatePsychlotron(psychlotron)) {
         using (var dc = new PsyDc()) {
             var newUser = new User() {
                 UserId = Guid.NewGuid(),
                 Username = username,
                 Password = password.HashDuJour(),
                 FirstName = firstname ?? "",
                 LastName = lastname ?? "",
                 Email = email ?? "",
                 Psychlotron = psychlotron ?? "",
                 DateCreated = DateTime.Now
             };
             dc.Users.InsertOnSubmit(newUser);
             dc.SubmitChanges();
             if (newUser.UserId.IsValidId()) {
                 return newUser.UserId;
             }
         }
     }
     return Guid.Empty;
 }
Esempio n. 2
0
 public static bool ReleasePsyauthentication(string psyauth)
 {
     if (psyauth.IsntEmpty()) {
         var guid = psyauth.ToGuid();
         if (guid.IsValidId()) {
             if (Psyauthenticated.ContainsKey(guid)) { // Seems like a good enough idea...
                 Psyauthenticated.Remove(guid);
             }
             using (var dc = new PsyDc()) {
                 var session = dc.Sessions.SingleOrDefault(s => s.SessionId == guid);
                 if (session != null) {
                     dc.Sessions.DeleteOnSubmit(session);
                     return dc.SubmitChanges_ResolveAll();
                 }
             }
         }
     }
     return false;
 }
Esempio n. 3
0
 public static bool SetPassword(Guid userId, string rawPassword)
 {
     using (var dc = new PsyDc()) {
         var user = dc.Users.SingleOrDefault(u => u.UserId.Equals(userId));
         if (user != null && rawPassword.IsntEmpty()) {
             user.Password = rawPassword.HashDuJour();
             return dc.SubmitChanges_ResolveAll();
         }
     }
     return false;
 }
Esempio n. 4
0
 public ContentDto SaveContent(string psyauth, ContentDto dto)
 {
     var userId = Auth.Psyauthenticate(psyauth);
     if (userId.IsValidId()) {
         var content = (Content)null;
         using (var dc = new PsyDc()) {
             if (dto.contentId.IsValidId()) {
                 content = dc.Contents.Single(c => c.ContentId.Equals(dto.contentId));
                 // This works well as a guard against visitors editing content, although in reality
                 // the ui should prevent it in the first place
                 if (content.Creator.Equals(userId)) {
                     content.ContentTitle = dto.title;
                     content.MimeType = dto.mimeType;
                     content.ContentAsString = dto.contentAsString;
                     dc.SubmitChanges_ResolveAll();
                 }
             }
             else {
                 content = Content.New();
                 content.Creator = userId;
                 content.ContentTitle = dto.title;
                 content.MimeType = dto.mimeType;
                 content.ContentAsString = dto.contentAsString;
                 dc.Contents.InsertOnSubmit(content);
                 dc.SubmitChanges_ResolveAll();
             }
         }
         return content.ToDto();
     }
     return null;
 }