Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.Clear();
            Config.Initialize();

            //var parsed = @"c:\jeromeyers\psychlotron\psychlotron.csv".ParseCsv(false);
            //var x = parsed;

            // Add me as a user and mark all existing content as mine
            using (var dc = new PsyDc()) {

                var x = new JeromeyersDotComPublisher();
                x.Publish();
                Console.ReadLine();
                // Change Password
                //foreach (var user in dc.Users.ToList()) {
                //	//

                //}
                //dc.SubmitChanges();

                // Create User

                // Set Content's User
                //var me = dc.Users.First();
                //var contents = dc.Contents.ToList();
                //contents.ForEach((c) => {
                //	c.User = me;
                //});
                //dc.SubmitChanges();
            }
        }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
0
 public Dictionary<Guid, ContentDto> GetAllContentIds(string psyauth)
 {
     var userId = Auth.Psyauthenticate(psyauth);
     if (userId.IsValidId()) {
         using (var dc = new PsyDc()) {
             return dc.Contents
                 .Where(c => c.Creator.Equals(userId) || c.PrivacyLevel == (byte)PrivacyLevel.Public).OrderBy(c => c.DateCreated)
                 .ToList()
                 .ToDictionary(c => c.ContentId, c => c.ToDto());
         }
     }
     return null;
 }
Exemplo n.º 4
0
 public static Guid NewPsyauth(User user)
 {
     using (var dc = new PsyDc()) {
         var session = new Session() {
             DateCreated = DateTime.Now,
             SessionId = Guid.NewGuid(),
             UserId = user.UserId
         };
         dc.Sessions.InsertOnSubmit(session);
         dc.SubmitChanges();
         if (session.SerialNumber > 0) {
             return session.SessionId;
         }
     }
     return Guid.Empty;
 }
Exemplo n.º 5
0
 public Stream ExportData(string psyauth)
 {
     var userId = Auth.Psyauthenticate(psyauth);
     if (userId.IsValidId()) {
         var result = new StringBuilder();
         using (var dc = new PsyDc()) {
             foreach (var content in dc.Contents.Where(c => c.Creator.Equals(userId))) {
                 result.AppendLine(SEPARATOR.Csv(content.ContentId, content.ContentTitle.Replace(",", COMMA), content.DateCreated, content.MimeType, content.ContentAsString.Replace(",", COMMA).Replace("\r\n", NEWLINE).Replace("\n", NEWLINE).Replace("\r", NEWLINE)));
             }
         }
         WebOperationContext.Current.OutgoingResponse.ContentType = "text/csv;";
         WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", "attachment; filename=psychlotron.csv");
         return new MemoryStream(Encoding.UTF8.GetBytes(result.ToString()));
     }
     return null;
 }
Exemplo n.º 6
0
 public static Guid Psyauthenticate(string psyauth)
 {
     // Gotta have some fun with the names, eh?
     if (psyauth.IsntEmpty()) {
         var guid = psyauth.ToGuid();
         if (guid.IsValidId()) {
             if (Psyauthenticated.ContainsKey(guid)) { // Seems like a good enough idea...
                 return Psyauthenticated[guid];
             }
             else {
                 using (var dc = new PsyDc()) {
                     var session = dc.Sessions.SingleOrDefault(s => s.SessionId == guid);
                     if (session != null) {
                         Psyauthenticated[guid] = session.UserId;
                         return session.UserId;
                     }
                 }
             }
         }
     }
     return Guid.Empty;
 }
Exemplo n.º 7
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;
 }
Exemplo n.º 8
0
 public static bool VerifyPassword(Guid userId, string candidate)
 {
     using (var dc = new PsyDc()) {
         var user = dc.Users.SingleOrDefault(u => u.UserId.Equals(userId));
         if (user != null && candidate.IsntEmpty()) {
             if (candidate.VerifyPassword(user.Password)) {
                 return true;
             }
         }
     }
     // TODO: Probably keep track of failed attempts once sercure-ity becomes a prior-ity
     throw new PsychlotronException("pw failed for {0}".Fmt(userId));
 }
Exemplo n.º 9
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;
 }
Exemplo n.º 10
0
 private UserDto InnerPsyauthLogin(string psyauth)
 {
     var result = new UserDto();
     var userId = Auth.Psyauthenticate(psyauth);
     if (userId.IsValidId()) {
         using (var dc = new PsyDc()) {
             var user = dc.Users.SingleOrDefault(u => u.UserId.Equals(userId));
             if (user != null) {
                 result = user.ToDto();
                 result.success = true;
                 result.psyauth = psyauth;
             }
         }
     }
     return result;
 }
Exemplo n.º 11
0
 private UserDto InnerLogin(string username, string password)
 {
     var result = new UserDto();
     using (var dc = new PsyDc()) {
         var user = dc.Users.SingleOrDefault(u => u.Username.ToLower() == username.ToLower());
         if (user != null) {
             if (Auth.VerifyPassword(user.UserId, password)) {
                 result = user.ToDto();
                 result.success = true;
                 result.psyauth = Auth.NewPsyauth(user).ToString();
             }
         }
     }
     return result;
 }
Exemplo n.º 12
0
 // Content
 public Dictionary<Guid, ContentDto> Search(string search, string psyauth)
 {
     var userId = Auth.Psyauthenticate(psyauth);
     if (userId.IsValidId()) {
         using (var dc = new PsyDc()) {
             var query = dc.Contents.AsQueryable();
             if (search == "all-content") {
                 query = query.Where(c => c.Creator.Equals(userId) || c.PrivacyLevel == (byte)PrivacyLevel.Public);
             }
             else if (search == "all-jemls") {
                 query = query.Where(c => (c.Creator.Equals(userId) || c.PrivacyLevel == (byte)PrivacyLevel.Public) && c.MimeType == "jeml");
             }
             return query.OrderBy(c => c.DateCreated).ToList().ToDictionary(c => c.ContentId, c => c.ToDto());
         }
     }
     return null;
 }
Exemplo n.º 13
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;
 }
Exemplo n.º 14
0
 // TODO: The bulk of login and logout probably belong in Auth
 public UserDto RegisterUser(RegisterUserRequest request)
 {
     var userId = Auth.CreateUser(request.username, request.password, request.firstName, request.lastName, request.email, request.psychlotron);
     if (userId.IsValidId()) {
         using (var dc = new PsyDc()) {
             var user = dc.Users.SingleOrDefault(u => u.UserId == userId);
             if (user != null) {
                 return InnerLogin(user.Username, request.password);
             }
         }
     }
     return new UserDto();
 }