Beispiel #1
0
 public void RegisterBusinessEntity(BusinessEntityRegistrationContract contract)
 {
     contract.MainContact.Password = passwordEncryption.EncryptPassword(contract.MainContact.Password);
     registrationRepository.RegisterBusinessEntity(contract);
 }
Beispiel #2
0
        protected override async ValueTask <EntityEntry <User> > AttachNewOrUpdateAsync(EntityEntry <User> entry, CancellationToken cancellationToken)
        {
            var entity   = entry.Entity;
            var password = entity.Password;

            if (null != entity.ClientApplication)
            {
                if (entity.ClientApplication.HasValidId())
                {
                    entity.ClientApplicationId = entity.ClientApplication.Id;
                }
                entity.ClientApplication = null;
            }

            if (null != entity.Avatar)
            {
                if (entity.Avatar.HasValidId())
                {
                    entity.AvatarId = entity.Avatar.Id;
                }
                entity.Avatar = null;
            }

            // Permissions
            var dbContext = EFCoreContext.DbContext;

            if (entity.HasValidId())
            {
                var fixedPermissions = new HashSet <UserPermission>();
                var toKeep           = new HashSet <int>();
                if (null != entity.Permissions)
                {
                    foreach (var rel in entity.Permissions)
                    {
                        var dbRel = await dbContext.Set <UserPermission>().Where(up => up.UserId == entity.Id && up.PermissionId == rel.PermissionId).FirstOrDefaultAsync(cancellationToken);

                        if (null == dbRel)
                        {
                            var dbRelEntry = await dbContext.AddAsync(new UserPermission
                            {
                                PermissionId = rel.PermissionId,
                                UserId       = entity.Id
                            });

                            dbRel = dbRelEntry.Entity;
                        }
                        fixedPermissions.Add(dbRel);
                        toKeep.Add(rel.PermissionId);
                    }
                }
                entity.Permissions = fixedPermissions;
                var toRemove = await dbContext.Set <UserPermission>().Where(up => up.UserId == entity.Id && !toKeep.Contains(up.PermissionId)).ToListAsync(cancellationToken);

                if (toRemove.Count > 0)
                {
                    dbContext.RemoveRange(toRemove);
                }
            }
            else
            {
                if (null != entity.Permissions)
                {
                    foreach (var rel in entity.Permissions)
                    {
                        rel.User = entity;
                    }
                }
                else
                {
                    entity.Permissions = new HashSet <UserPermission>();
                }
            }

            var e = await base.AttachNewOrUpdateAsync(entry, cancellationToken);

            if (e.State == EntityState.Added)
            {
                if (string.IsNullOrEmpty(password))
                {
                    throw new InvalidOperationException("No password specified.");
                }
                var encryptedPassword = _passwordEncryption.EncryptPassword(password);
                e.Entity.Password            = encryptedPassword.PasswordHash;
                e.Entity.Salt                = encryptedPassword.Salt;
                e.Entity.ClientApplicationId = _currentClientApplication.Id;
            }
            else
            {
                if (string.IsNullOrEmpty(password))
                {
                    var dbValues = await e.GetDatabaseValuesAsync(cancellationToken);

                    e.Entity.Password = dbValues.GetValue <string>(nameof(User.Password));
                    e.Entity.Salt     = dbValues.GetValue <string>(nameof(User.Salt));
                    e.Property(x => x.Password).IsModified = false;
                    e.Property(x => x.Salt).IsModified     = false;
                }
                else
                {
                    var encryptedPassword = _passwordEncryption.EncryptPassword(password);
                    e.Entity.Password = encryptedPassword.PasswordHash;
                    e.Entity.Salt     = encryptedPassword.Salt;
                }
            }
            return(e);
        }