public void AddContactGroupMember(contactGroupMember member) { // check that member doesn't already exist contactGroupMember m = member; var exists = db.contactGroupMembers.Where(x => x.groupid == m.groupid && x.userid == m.userid) .SingleOrDefault(); if (exists == null) { db.contactGroupMembers.InsertOnSubmit(member); db.SubmitChanges(); } else { member = exists; } }
public void UpdateContactGroupMembers(long subdomainid, long groupid, string[] userids) { // get existing ones first in filter var result = db.contactGroupMembers .Where(x => x.contactGroup.subdomainid == subdomainid && x.groupid == groupid); db.contactGroupMembers.DeleteAllOnSubmit(result); // add new ones foreach (var userid in userids) { var cf = new contactGroupMember(); cf.userid = long.Parse(userid); cf.groupid = groupid; db.contactGroupMembers.InsertOnSubmit(cf); } db.SubmitChanges(); }
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())); }