예제 #1
0
        public User ModelToEntity(UserSessionModel admin, UserModel model)
        {
            var entity = GetEntity(admin, model);

            if (this.Response.HasError)
            {
                return(null);
            }

            entity.Email = Utilities.Trim(model.Email);

            entity.FirstName = Utilities.Trim(model.FirstName);

            entity.MiddleName = Utilities.Trim(model.MiddleName);

            entity.LastName = Utilities.Trim(model.LastName);

            entity.MiddleName = Utilities.Trim(model.MiddleName);

            entity.LastName = Utilities.Trim(model.LastName);

            entity.UseBusinessAddress = model.UseBusinessAddress;

            entity.IsRegistering = model.IsRegistering;                                // not saved in DB but only for reference

            entity.IsNewBusiness = model.ExistingBusiness == ExistingBusinessEnum.New; // not saved in DB but only for reference

            contactService.BeginPropertyReference(this, "Contact");
            entity.Contact = contactService.ModelToEntity(model.Contact);
            contactService.EndPropertyReference();

            //Check address if you are not using business address
            if (model.UseBusinessAddress == false || entity.IsNewBusiness)
            {
                addressService.BeginPropertyReference(this, "Address");
                entity.Address = addressService.ModelToEntity(model.Address);
                addressService.EndPropertyReference();
            }

            if (entity.IsRegistering)
            {
                entity.Salt = Crypto.GenerateSalt();

                entity.Password = Crypto.Hash(model.Password, entity.Salt);

                if (!entity.IsNewBusiness)
                {
                    var dweb = new DaikinServices();

                    // Lookup by CRM Account Id and Daikin City Id
                    entity.Business = this.Db.GetBusinessByAccountId(model.Business.AccountId);
                    if (entity.Business == null &&
                        !string.IsNullOrEmpty(model.Business.DaikinCityId))
                    {
                        entity.Business = this.Db.GetBusinessByAccountId(model.Business.DaikinCityId);
                    }

                    if (entity.Business == null)
                    {
                        // Create record so that the business rules can be applied and correct error message produced
                        // i.e account verify will take place in the business rules
                        entity.Business = new Business
                        {
                            BusinessTypeId = (model.Business.BusinessTypeId == null) ? BusinessTypeEnum.Other : (BusinessTypeEnum)model.Business.BusinessTypeId,
                            AccountId      = model.Business.AccountId,
                            BusinessName   = model.Business.BusinessName
                        };
                    }

                    this.Response.PropertyReference = "";
                }
                else// is New Business
                {
                    //Copy address to the business
                    model.Business.Address = model.Address;

                    model.Business.BusinessId = null; // clear to make sure new record is added

                    businessService.BeginPropertyReference(this, "Business");
                    entity.Business = businessService.ModelToEntity(admin, model.Business, false);
                    if (model.Business.ParentBusinessId != null && model.Business.ParentBusinessId != 0 && entity.Business.BusinessId != 0)
                    {
                        //add BusinessLinks record
                        BusinessLinkModel businessLinkModel = new BusinessLinkModel()
                        {
                            BusinessId       = (long)entity.Business.BusinessId,
                            ParentBusinessId = model.Business.ParentBusinessId
                        };
                        businessLinkService.ModelToEntity(businessLinkModel);
                    }
                    businessService.EndPropertyReference();

                    // Setup default business permissions
                    if (!string.IsNullOrEmpty(entity.Business.BusinessId.ToString()) &&
                        !string.IsNullOrEmpty(entity.Business.BusinessTypeId.ToString()))
                    {
                        var bId  = entity.Business.BusinessId;
                        var btId = entity.Business.BusinessTypeId;
                        Db.ReplacePermissions(EntityEnum.BusinessType, (long)btId, EntityEnum.Business, bId, PermissionTypeEnum.Brand);
                        Db.ReplacePermissions(EntityEnum.BusinessType, (long)btId, EntityEnum.Business, bId, PermissionTypeEnum.CityArea);
                        Db.ReplacePermissions(EntityEnum.BusinessType, (long)btId, EntityEnum.Business, bId, PermissionTypeEnum.ProductFamily);
                        Db.ReplacePermissions(EntityEnum.BusinessType, (long)btId, EntityEnum.Business, bId, PermissionTypeEnum.Tool);
                    }
                }
            }
            else
            {
                // Own user cant change user type.
                if (admin.UserId != model.UserId)
                {
                    entity.UserTypeId = (UserTypeEnum)model.UserTypeId;
                }

                entity.BusinessId = model.Business.BusinessId;

                if (entity.BusinessId.HasValue) // Need business for business permission rules
                {
                    this.Db.GetBusinessByBusinessId(entity.BusinessId.Value);
                }

                if (model.Business.ParentBusinessId != 0)
                {
                    businessService.UpdateParentBusiness(model.Business);
                }

                if (this.Context.Entry(entity).State == EntityState.Added)
                {
                    entity.Salt = Crypto.GenerateSalt();

                    entity.Password = Crypto.Hash("%^$ggtfr", entity.Salt);

                    entity.Enabled = true;
                }

                entity.ShowPricing = model.ShowPricing;

                entity.Enabled = model.Enabled ?? entity.Enabled;

                entity.Approved = model.Approved ?? entity.Approved;

                if (model.UserId != admin.UserId) // cant update your own access
                {
                    permissionService.ApplyBusinessRules(entity, model, admin);
                }
            }

            return(entity);
        }
 public CRMAccountImport(DPOContext dpoContext)
 {
     context        = dpoContext;
     daikinServices = new DaikinServices();
     this.Db        = new Repository(context);
 }