public UserDto Register(string name, string passwordOrHash, string salt = null) { User user = GetUserEntity(name, passwordOrHash, salt); user = ((NotesContext)Context).Users.Add(user); if (user == null) { throw new Exception("Couldn't add user, maybe username is taken? lol idk glhfdd"); } Context.SaveChanges(); if (!CheckUser(user)) { throw new Exception("User should have been added, but it either wasn't found, was 'removed,' or is invalid"); } user.Token = GenerateToken(user.Name); NotesPrincipal principal = Thread.CurrentPrincipal as NotesPrincipal; if (principal == null) { throw new Exception("User should have been registered and valid, but was unable to login automatically... Maybe try to login manually?"); } return(Mapper.Map <UserDto>(user)); }
public UserDto Authenticate(string name, string passwordOrHash, string salt = null) { User user = GetUserEntity(name, passwordOrHash, salt); if (!CheckUser(user)) { throw new Exception($"No valid user {name} exists with the given password"); } user.Token = GenerateToken(user.Name); NotesPrincipal principal = Thread.CurrentPrincipal as NotesPrincipal; if (principal == null) { throw new Exception($"Unable to login as {name}"); } return(Mapper.Map <UserDto>(user)); }
public override int SaveChanges() { // Get all modified entities in lists so we can work on them after saving the changes List <DbEntityEntry> addedMetadataEntities = ChangeTracker.Entries() .Where(e => !(e.Entity is IAudit) && (e.Entity is IEntity) && e.State == EntityState.Added) .ToList(); List <DbEntityEntry> modifiedMetadataEntities = ChangeTracker.Entries() .Where(e => !(e.Entity is IAudit) && (e.Entity is IEntity) && e.State == EntityState.Modified) .ToList(); List <DbEntityEntry> deletedMetadataEntities = ChangeTracker.Entries() .Where(e => !(e.Entity is IAudit) && (e.Entity is IEntity) && e.State == EntityState.Deleted) .ToList(); base.SaveChanges(); var now = DateTime.UtcNow; NotesPrincipal principal = Thread.CurrentPrincipal as NotesPrincipal; User CurrentUser = string.IsNullOrEmpty(principal?.Identity?.Name) ? Users.Single(e => e.IsSystemOnly) : Users.DefaultIfEmpty(null).Single(e => !e.IsRemoved && !e.IsSystemOnly && e.Name == principal.Identity.Name); // Insert an AuditAdd entity for each added entity foreach (var added in addedMetadataEntities) { var stream = new MemoryStream(); // Make a serializer for the added entity by getting the DisplayName attribute // and making a type from the resulting string so we can record original values var serializer = new DataContractJsonSerializer( Type.GetType( added.Entity .GetType() .GetCustomAttributes(typeof(DisplayNameAttribute), true) .Select(a => ((DisplayNameAttribute)a).DisplayName) //.DefaultIfEmpty(added.Entity.GetType().Name) .First() ) ); serializer.WriteObject(stream, added.Entity); // Populate the IsRemoved property of any removables? Is this necessary? //((IRemovable)added.Entity).IsRemoved = false; AuditAdds.Add(new AuditAdd { Timestamp = now, User = CurrentUser, UserId = CurrentUser.Id, Entity = added.Entity.GetType().Name, EntityId = ((IEntity)added.Entity).Id, Json = Encoding.ASCII.GetString(stream.ToArray()) }); } // Insert AuditUpdate entities for each updated entity property // Does this actually only do the properties that have been changed? Or all properties...? foreach (var modified in modifiedMetadataEntities) { foreach (string prop in modified.OriginalValues.PropertyNames) { string from = modified.OriginalValues[prop].ToString(); string to = modified.CurrentValues[prop].ToString(); AuditUpdates.Add(new AuditUpdate { Timestamp = now, User = CurrentUser, UserId = CurrentUser.Id, Entity = modified.Entity.GetType().Name, EntityId = ((IEntity)modified.Entity).Id, Field = prop, From = from, To = to }); } } // Insert an AuditDelete entity for each deleted entity foreach (var deleted in deletedMetadataEntities) { AuditDeletes.Add(new AuditDelete { Timestamp = now, User = CurrentUser, UserId = CurrentUser.Id, Entity = deleted.Entity.GetType().Name, EntityId = ((IEntity)deleted.Entity).Id }); } return(base.SaveChanges()); }