Exemplo n.º 1
0
        public ActionResult Create(long?profilePhotoID, string password, string organisationPhotoID, string address, string city,
                                   long?citySelected, string coPhone, int permissions,
                                   string companyName, string contactList, int?country, string email, string fax, string firstName,
                                   string gender, string lastName, string notes, string phone, string postcode, string title, long?existingOrg,
                                   string states_canadian, string states_other, string states_us, bool sendemail)
        {
            // add organisation even if fields are empty
            if (!string.IsNullOrEmpty(email))
            {
                // check if email already added
                var emailExist = repository.IsEmailInUse(email, subdomainid.Value);
                if (emailExist)
                {
                    return(Json("Email has already been added".ToJsonFail()));
                }
            }

            organisation o;
            long         addedOrgID;

            if (!existingOrg.HasValue)
            {
                o = new organisation
                {
                    subdomain = subdomainid.Value,
                    address   = address.Trim(),
                    phone     = coPhone,
                    name      = companyName,
                    fax       = fax,
                    postcode  = postcode
                };
                if (!string.IsNullOrEmpty(organisationPhotoID))
                {
                    o.logo = long.Parse(organisationPhotoID);
                }

                if (citySelected.HasValue)
                {
                    o.city = citySelected.Value;
                }
                else if (!string.IsNullOrEmpty(city))
                {
                    o.city = repository.AddCity(city).id;
                }

                if (country != null)
                {
                    o.country = country;
                    o.state   = AddressHandler.GetState(country, states_us,
                                                        states_canadian, states_other);
                }

                addedOrgID = repository.AddOrganisation(o);

                // update shipping and billing addresses
                var addressHandler = new AddressHandler(o, repository);
                addressHandler.CopyShippingAndBillingAddressFromOrgAddress("", "");
            }
            else
            {
                o = repository.GetOrganisation(existingOrg.Value, subdomainid.Value);
                if (o == null)
                {
                    return(SendJsonErrorResponse("Company is invalid"));
                }
                addedOrgID = o.id;
            }

            // add user
            var u = new user
            {
                created      = DateTime.UtcNow,
                role         = (int)UserRole.USER,
                email        = email,
                passwordHash = Crypto.Utility.ComputePasswordHash(email + password),
                firstName    = firstName,
                gender       = gender,
                lastName     = lastName,
                notes        = notes,
                phoneNumber  = phone,
                title        = title,
                organisation = addedOrgID,
                viewid       = Crypto.Utility.GetRandomString()
            };

            // only allow user to create user with permissions equal to or less than themselves
            var currentuser       = repository.GetUserById(sessionid.Value, subdomainid.Value);
            var allowedPermission = currentuser.permissions & permissions;

            u.permissions = allowedPermission;

            try
            {
                if (profilePhotoID.HasValue)
                {
                    u.profilePhoto = profilePhotoID.Value;
                }

                repository.AddUser(u);


                // need to update entry in images table too since contextid will be the site creator's
                if (profilePhotoID.HasValue)
                {
                    var dbImage = repository.GetImage(profilePhotoID.Value);
                    if (dbImage != null)
                    {
                        dbImage.contextID = u.id;
                    }
                }

                // log activity
                repository.AddActivity(sessionid.Value,
                                       new ActivityMessage(u.id, sessionid,
                                                           ActivityMessageType.CONTACT_NEW,
                                                           new HtmlLink(u.ToEmailName(true), u.id).ToContactString()), subdomainid.Value);

                // update total contacts count
                repository.UpdateCounters(subdomainid.Value, 1, CounterType.CONTACTS_PRIVATE);

                // add contact list filter
                if (!string.IsNullOrEmpty(contactList))
                {
                    var cf = new contactGroupMember()
                    {
                        groupid = long.Parse(contactList),
                        userid  = u.id
                    };
                    repository.AddContactGroupMember(cf);
                }

                // email contact that was just added
                if (!string.IsNullOrEmpty(email) && sendemail)
                {
                    var me        = repository.GetUserById(sessionid.Value, subdomainid.Value);
                    var viewmodel = new ContactNewViewModel
                    {
                        creatorEmail = me.email,
                        creatorName  = me.ToEmailName(true),
                        hostName     = accountHostname,
                        email        = email,
                        password     = password,
                        note         = notes,
                        profile      = u.ToProfileUrl()
                    };

                    // link to view profile
                    this.SendEmail(EmailViewType.CONTACT_NEWENTRY, viewmodel, "New Profile created", u.GetEmailAddress(), u.ToFullName(), u);
                }
            }
            catch (Exception ex)
            {
                return(SendJsonErrorResponse(ex));
            }
            // return contact ID & org ID
            return(Json(new { uid = u.id, oid = addedOrgID }.ToJsonOKData()));
        }
Exemplo n.º 2
0
        public ActionResult Update(long?id, string email, string address, string city, long?citySelected, string coPhone,
                                   string companyName, IEnumerable <int?> country, string fax, string firstName, int?permissions,
                                   string gender, string lastName, string notes, string phone, string postcode, string title, string password,
                                   IEnumerable <string> states_canadian, IEnumerable <string> states_other, IEnumerable <string> states_us,
                                   string billing_first_name, string billing_last_name, string billing_company, string billing_address, string billing_city, long?billing_citySelected,
                                   string billing_postcode, string billing_phone,
                                   string shipping_first_name, string shipping_last_name, string shipping_company, string shipping_address, string shipping_city, long?shipping_citySelected, string shipping_postcode, string shipping_phone)
        {
            if (!id.HasValue)
            {
                return(SendJsonErrorResponse("Missing ID"));
            }

            try
            {
                var contact = repository.GetContact(subdomainid.Value, id.Value);

                if (contact == null)
                {
                    return(SendJsonErrorResponse("Missing ID"));
                }

                var original = contact.ToModel(sessionid, subdomainid.Value);

                // no need to take into account whether an organisation is there because it will always be created
                contact.organisation1.address = address.Trim();
                if (citySelected.HasValue)
                {
                    var mcity = repository.GetCity(citySelected.Value);
                    contact.organisation1.MASTERcity = mcity;
                }
                else if (!string.IsNullOrEmpty(city))
                {
                    contact.organisation1.MASTERcity = repository.AddCity(city);
                }
                if (coPhone != null)
                {
                    contact.organisation1.phone = coPhone;
                }
                if (companyName != null)
                {
                    contact.organisation1.name = companyName;
                }
                if (country != null)
                {
                    contact.organisation1.country = country.ElementAtOrDefault(0);
                    contact.organisation1.state   = AddressHandler.GetState(country.ElementAtOrDefault(0), states_us.ElementAtOrDefault(0), states_canadian.ElementAtOrDefault(0), states_other.ElementAtOrDefault(0));
                }
                if (fax != null)
                {
                    contact.organisation1.fax = fax;
                }

                if (email != null)
                {
                    contact.email = email;
                }
                if (firstName != null)
                {
                    contact.firstName = firstName;
                }
                if (gender != null)
                {
                    contact.gender = gender;
                }
                if (lastName != null)
                {
                    contact.lastName = lastName;
                }
                if (phone != null)
                {
                    contact.phoneNumber = phone;
                }
                if (postcode != null)
                {
                    contact.organisation1.postcode = postcode;
                }

                // handle addresses
                var addressHandler = new AddressHandler(contact.organisation1, repository);
                addressHandler.SetShippingAndBillingAddresses(billing_first_name,
                                                              billing_last_name,
                                                              billing_company,
                                                              billing_address,
                                                              billing_city,
                                                              billing_citySelected,
                                                              billing_postcode,
                                                              billing_phone,
                                                              country.ElementAtOrDefault(1),
                                                              states_canadian.ElementAtOrDefault(1),
                                                              states_other.ElementAtOrDefault(1),
                                                              states_us.ElementAtOrDefault(1),
                                                              shipping_first_name,
                                                              shipping_last_name,
                                                              shipping_company,
                                                              shipping_address,
                                                              shipping_city,
                                                              shipping_citySelected,
                                                              shipping_postcode,
                                                              shipping_phone,
                                                              country.ElementAtOrDefault(2),
                                                              states_canadian.ElementAtOrDefault(2),
                                                              states_other.ElementAtOrDefault(2),
                                                              states_us.ElementAtOrDefault(2),
                                                              false);

                if (title != null)
                {
                    contact.title = title;
                }

                if (!string.IsNullOrEmpty(password))
                {
                    // password specified
                    contact.passwordHash = Crypto.Utility.ComputePasswordHash(email + password);
                }
                else
                {
                    // password removed
                    contact.passwordHash = null;
                }

                // list of fields that are allowed to be modified
                if (notes != null)
                {
                    contact.notes = notes;
                }

                // handle permissions
                if (permissions.HasValue)
                {
                    contact.permissions = permissions;
                }

                repository.AddActivity(sessionid.Value,
                                       new ActivityMessage(id.Value, sessionid,
                                                           ActivityMessageType.CONTACT_UPDATED,
                                                           new HtmlLink(contact.ToEmailName(true), id.Value).ToContactString()), subdomainid.Value);

                repository.Save();
#if LUCENE
                // update search index
                var indexer = new LuceneWorker(db, MASTERdomain.ToIdName());
                indexer.AddToIndex(LuceneIndexType.CONTACTS, contact);
#endif
                // get changed and store in database
                var changed  = contact.ToModel(sessionid, subdomainid.Value);
                var comparer = new CompareObject();
                var diff     = comparer.Compare(original, changed);
                if (diff.Count != 0)
                {
                    repository.AddChangeHistory(sessionid.Value, contact.id, ChangeHistoryType.CONTACT, diff);
                }
            }
            catch (Exception ex)
            {
                return(SendJsonErrorResponse(ex));
            }
            return(Json(id.ToJsonOKData()));
        }
Exemplo n.º 3
0
        public ActionResult UpdateProfile(string address, string city, string citySelected, string coPhone,
                                          string companyName, int?country, string fax, string firstName,
                                          string gender, string lastName, string notes,
                                          string phone, string postcode, string title, string currency, string timezone,
                                          string email, string states_canadian, string states_other, string states_us)
        {
            var ownerid = sessionid.Value;

            try
            {
                var profile = repository.GetUserById(ownerid, subdomainid.Value);
                if (profile == null)
                {
                    return(SendJsonErrorResponse("Cannot find profile"));
                }

                // no need to take into account whether an organisation is there because it will always be created
                profile.organisation1.address = address;
                if (!string.IsNullOrEmpty(citySelected))
                {
                    profile.organisation1.city = int.Parse(citySelected);
                }
                else if (!string.IsNullOrEmpty(city))
                {
                    profile.organisation1.city = repository.AddCity(city).id;
                }
                profile.organisation1.phone = coPhone;
                profile.organisation1.name  = companyName;
                profile.organisation1.fax   = fax;

                if (country != null)
                {
                    profile.organisation1.country = country;
                    profile.organisation1.state   = AddressHandler.GetState(country, states_us, states_canadian, states_other);
                }

                profile.firstName              = firstName;
                profile.gender                 = gender;
                profile.lastName               = lastName;
                profile.notes                  = notes;
                profile.phoneNumber            = phone;
                profile.organisation1.postcode = postcode;
                profile.title                  = title;

                if (!string.IsNullOrEmpty(email) && email != profile.email)
                {
                    profile.email = email.Trim();

                    var password = Crypto.Utility.GetRandomString();

                    // save password hash
                    var hash = Crypto.Utility.ComputePasswordHash(email + password);
                    profile.passwordHash = hash;

                    // set flag
                    profile.settings |= (int)UserSettings.PASSWORD_RESET;

                    // email new password to user
                    var data = new ViewDataDictionary()
                    {
                        { "password", password }
                    };
                    EmailHelper.SendEmail(EmailViewType.ACCOUNT_PASSWORD_RESET, data, "Password Reset", email, profile.ToFullName(), null);
                }

                if (permission.HasFlag(UserPermission.NETWORK_SETTINGS))
                {
                    if (!string.IsNullOrEmpty(timezone))
                    {
                        profile.timezone = timezone;
                    }

                    if (!string.IsNullOrEmpty(currency))
                    {
                        profile.organisation1.MASTERsubdomain.currency = int.Parse(currency);
                    }
                }

                repository.Save();
                CacheHelper.Instance.invalidate_dependency(DependencyType.products_subdomain, subdomainid.Value.ToString());
                CacheHelper.Instance.invalidate_dependency(DependencyType.organisation, subdomainid.Value.ToString());
#if LUCENE
                // update index
                var indexer = new LuceneWorker(db, profile.organisation1.MASTERsubdomain.ToIdName());
                indexer.AddToIndex(LuceneIndexType.CONTACTS, profile);
#endif
            }
            catch (Exception ex)
            {
                return(SendJsonErrorResponse(ex));
            }
            // will be intepreted as an error if Content() is used
            return(Json(OPERATION_SUCCESSFUL.ToJsonOKData()));
        }