/// <summary> /// Accepts invitation. /// </summary> protected void btnResend_OnCommand(object sender, CommandEventArgs e) { if (e.CommandName == "resend") { try { // Get invitation info object int invitationId = ValidationHelper.GetInteger(e.CommandArgument, 0); InvitationInfo invitation = InvitationInfoProvider.GetInvitationInfo(invitationId); if (invitation != null) { // Send invitation e-mail InvitationInfoProvider.SendInvitationEmail(invitation, CMSContext.CurrentSiteName); lblInfo.Text = ResendSuccess + "<br /><br />"; } } catch { lblInfo.Text = ResendFailed + "<br /><br />";; lblInfo.CssClass = "InvitationErrorLabel"; } lblInfo.Visible = true; } }
/// <summary> /// Creates new invitation to group. Called when the "Add member to group" button is pressed. /// Expects the CreateGroup method to be run first. /// </summary> private bool CreateInvitation() { // Get the group GroupInfo group = GroupInfoProvider.GetGroupInfo("MyNewGroup", CMSContext.CurrentSiteName); if (group != null) { // Create new invitation InvitationInfo newInvitation = new InvitationInfo(); // Set properties newInvitation.InvitationComment = "Invitation created by API example."; newInvitation.InvitationGroupID = group.GroupID; newInvitation.InvitationUserEmail = "*****@*****.**"; newInvitation.InvitedByUserID = CMSContext.CurrentUser.UserID; newInvitation.InvitationCreated = DateTime.Now; newInvitation.InvitationValidTo = DateTime.Now.AddDays(1); // Save object InvitationInfoProvider.SetInvitationInfo(newInvitation); return(true); } return(false); }
/// <summary> /// Deletes invitation. /// </summary> protected void btnDelete_OnCommand(object sender, CommandEventArgs e) { if (e.CommandName == "delete") { int invitationId = ValidationHelper.GetInteger(e.CommandArgument, 0); InvitationInfoProvider.DeleteInvitationInfo(invitationId); BindData(); } }
/// <summary> /// Creates invitation for specified user. /// </summary> /// <param name="groupInfo">Inviting group</param> /// <param name="invitedUser">User info</param> private InvitationInfo InviteUser(GroupInfo groupInfo, UserInfo invitedUser) { // Check whether group exists if (groupInfo == null) { return(null); } int userId = (invitedUser != null) ? invitedUser.UserID : 0; int currentUserId = MembershipContext.AuthenticatedUser.UserID; // User cannot invite herself if (userId == currentUserId) { return(null); } // Check whether user is in group or is invited if ((!GroupMemberInfoProvider.IsMemberOfGroup(userId, Group.GroupID) && !InvitationInfoProvider.InvitationExists(userId, Group.GroupID)) || radNewUser.Checked) { // Create new info object var invitation = new InvitationInfo { InvitationComment = txtComment.Text, InvitationCreated = DateTime.Now, InvitationGroupID = GroupID, InvitationGUID = new Guid(), InvitationLastModified = DateTime.Now, InvitedByUserID = currentUserId }; // Create 'Valid to' value if set in settings var validTo = SettingsKeyInfoProvider.GetIntValue(SiteContext.CurrentSiteName + ".CMSInvitationValidity"); if (validTo > 0) { invitation.InvitationValidTo = DateTime.Now.AddDays(validTo); } if (radSiteMember.Checked) { invitation.InvitedUserID = userId; } else { invitation.InvitationUserEmail = txtEmail.Text; } InvitationInfoProvider.SetInvitationInfo(invitation); return(invitation); } return(null); }
/// <summary> /// Binds data to repeater. /// </summary> private void BindData() { if (UserName != String.Empty) { DataSet invitations = InvitationInfoProvider.GetMyInvitations("InvitedUserID IN (SELECT UserID FROM CMS_User WHERE UserName='******'", "''") + "')", "InvitationCreated"); rptMyInvitations.DataSource = invitations; rptMyInvitations.DataBind(); // Hide control if no data if (DataHelper.DataSourceIsEmpty(invitations) && (HideControlForZeroRows)) { Visible = false; rptMyInvitations.Visible = false; } } }
/// <summary> /// Binds data to repeater. /// </summary> private void BindData() { if (UserName != String.Empty) { DataSet invitations = InvitationInfoProvider.GetMySentInvitations("InvitedByUserID IN (SELECT UserID FROM CMS_User WHERE UserName='******')", "InvitationCreated"); rptMySentInvitations.DataSource = invitations; rptMySentInvitations.DataBind(); // Hide control if no data if (DataHelper.DataSourceIsEmpty(invitations) && (HideControlForZeroRows)) { Visible = false; rptMySentInvitations.Visible = false; } } }
/// <summary> /// Accepts invitation. /// </summary> protected void btnAccept_OnCommand(object sender, CommandEventArgs e) { if (e.CommandName == "accept") { // Accept int invitationId = ValidationHelper.GetInteger(e.CommandArgument, 0); InvitationInfo invitation = InvitationInfoProvider.GetInvitationInfo(invitationId); // Check if time is correct if ((invitation.InvitationValidTo == DateTimeHelper.ZERO_TIME) || (invitation.InvitationValidTo >= DateTime.Now)) { GroupInfo group = GroupInfoProvider.GetGroupInfo(invitation.InvitationGroupID); if (group != null) { // Check if invitation is from group member if (!GroupMemberInfoProvider.IsMemberOfGroup(invitation.InvitedUserID, invitation.InvitationGroupID)) { // Transfer user name to user info if (userInfo == null) { userInfo = UserInfoProvider.GetUserInfo(UserName); } if (userInfo != null) { // Create group member info object GroupMemberInfo groupMember = new GroupMemberInfo(); groupMember.MemberInvitedByUserID = invitation.InvitedByUserID; groupMember.MemberUserID = userInfo.UserID; groupMember.MemberGroupID = invitation.InvitationGroupID; groupMember.MemberJoined = DateTime.Now; // Set proper status depending on group settings switch (group.GroupApproveMembers) { case GroupApproveMembersEnum.ApprovedCanJoin: groupMember.MemberStatus = GroupMemberStatus.WaitingForApproval; lblInfo.Text = MemberWaiting.Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(group.GroupDisplayName)) + "<br /><br />"; break; default: groupMember.MemberApprovedByUserID = invitation.InvitedByUserID; groupMember.MemberApprovedWhen = DateTime.Now; groupMember.MemberStatus = GroupMemberStatus.Approved; lblInfo.Text = MemberJoined.Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(group.GroupDisplayName)) + "<br /><br />"; break; } // Store info object to database GroupMemberInfoProvider.SetGroupMemberInfo(groupMember); // Delete all invitations to specified group for specified user string whereCondition = "InvitationGroupID = " + invitation.InvitationGroupID + " AND (InvitedUserID=" + userInfo.UserID + ")"; InvitationInfoProvider.DeleteInvitations(whereCondition); // Log activity LogJoinActivity(groupMember, group); } } else { // Show error message and delete invitation lblInfo.Text = UserIsAlreadyMember.Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(group.GroupDisplayName)) + "<br /><br />"; lblInfo.CssClass = "InvitationErrorLabel"; InvitationInfoProvider.DeleteInvitationInfo(invitation); } } else { // Show error message and delete invitation lblInfo.Text = GroupNoLongerExists + "<br /><br />"; lblInfo.CssClass = "InvitationErrorLabel"; InvitationInfoProvider.DeleteInvitationInfo(invitation); } } else { // Show error message and delete invitation lblInfo.Text = InvitationIsNotValid + "<br /><br />"; lblInfo.CssClass = "InvitationErrorLabel"; InvitationInfoProvider.DeleteInvitationInfo(invitation); } lblInfo.Visible = true; BindData(); } }
/// <summary> /// Initializes the control properties. /// </summary> protected void SetupControl() { if (StopProcessing) { // Do nothing } else { if (!RequestHelper.IsPostBack()) { // If user is public if (MembershipContext.AuthenticatedUser.IsPublic()) { // Get logon URL string logonUrl = AuthenticationHelper.GetSecuredAreasLogonPage(SiteContext.CurrentSiteName); logonUrl = DataHelper.GetNotEmpty(LoginURL, logonUrl); // Create redirect URL logonUrl = UrlResolver.ResolveUrl(logonUrl) + "?ReturnURL=" + HttpUtility.UrlEncode(RequestContext.CurrentURL); URLHelper.Redirect(logonUrl); } else { // Get invitation by GUID Guid invitationGuid = QueryHelper.GetGuid("invitationguid", Guid.Empty); if (invitationGuid != Guid.Empty) { InvitationInfo invitation = InvitationInfoProvider.GetInvitationInfo(invitationGuid); if (invitation != null) { // Check if invitation is valid if ((invitation.InvitationValidTo == DateTimeHelper.ZERO_TIME) || (invitation.InvitationValidTo >= DateTime.Now)) { GroupInfo group = GroupInfoProvider.GetGroupInfo(invitation.InvitationGroupID); if (group != null) { // Check whether current user is the user who should be invited if ((invitation.InvitedUserID > 0) && (invitation.InvitedUserID != MembershipContext.AuthenticatedUser.UserID)) { lblInfo.CssClass = "InvitationErrorLabel"; lblInfo.Text = InvitationIsNotValid; lblInfo.Visible = true; return; } // If user was invited by e-mail if (invitation.InvitedUserID == 0) { invitation.InvitedUserID = MembershipContext.AuthenticatedUser.UserID; } if (!GroupMemberInfoProvider.IsMemberOfGroup(invitation.InvitedUserID, invitation.InvitationGroupID)) { // Create group member info object GroupMemberInfo groupMember = new GroupMemberInfo(); groupMember.MemberInvitedByUserID = invitation.InvitedByUserID; groupMember.MemberUserID = MembershipContext.AuthenticatedUser.UserID; groupMember.MemberGroupID = invitation.InvitationGroupID; groupMember.MemberJoined = DateTime.Now; // Set proper status depending on grouo settings switch (group.GroupApproveMembers) { // Only approved members can join case GroupApproveMembersEnum.ApprovedCanJoin: groupMember.MemberStatus = GroupMemberStatus.WaitingForApproval; lblInfo.Text = MemberWaiting.Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(group.GroupDisplayName)); break; // Only invited members case GroupApproveMembersEnum.InvitedWithoutApproval: // Any site members can join case GroupApproveMembersEnum.AnyoneCanJoin: groupMember.MemberApprovedWhen = DateTime.Now; groupMember.MemberStatus = GroupMemberStatus.Approved; lblInfo.Text = MemberJoined.Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(group.GroupDisplayName)); break; } // Store info object to database GroupMemberInfoProvider.SetGroupMemberInfo(groupMember); // Handle sending e-mails if (SendEmailToInviter || SendDefaultGroupEmails) { UserInfo sender = UserInfoProvider.GetFullUserInfo(groupMember.MemberUserID); UserInfo recipient = UserInfoProvider.GetFullUserInfo(groupMember.MemberInvitedByUserID); if (SendEmailToInviter) { EmailTemplateInfo template = EmailTemplateProvider.GetEmailTemplate("Groups.MemberAcceptedInvitation", SiteContext.CurrentSiteName); // Resolve macros MacroResolver resolver = MacroContext.CurrentResolver; resolver.SetAnonymousSourceData(sender, recipient, group, groupMember); resolver.SetNamedSourceData("Sender", sender); resolver.SetNamedSourceData("Recipient", recipient); resolver.SetNamedSourceData("Group", group); resolver.SetNamedSourceData("GroupMember", groupMember); if (!String.IsNullOrEmpty(recipient.Email) && !String.IsNullOrEmpty(sender.Email)) { // Send e-mail EmailMessage message = new EmailMessage(); message.EmailFormat = EmailFormatEnum.Default; message.Recipients = recipient.Email; message.From = SettingsKeyInfoProvider.GetValue(SiteContext.CurrentSiteName + ".CMSNoreplyEmailAddress"); EmailSender.SendEmailWithTemplateText(SiteContext.CurrentSiteName, message, template, resolver, false); } } if (SendDefaultGroupEmails) { // Send join or leave notification if (group.GroupSendJoinLeaveNotification && (groupMember.MemberStatus == GroupMemberStatus.Approved)) { GroupMemberInfoProvider.SendNotificationMail("Groups.MemberJoin", SiteContext.CurrentSiteName, groupMember, true); GroupMemberInfoProvider.SendNotificationMail("Groups.MemberJoinedConfirmation", SiteContext.CurrentSiteName, groupMember, false); } // Send 'waiting for approval' notification if (group.GroupSendWaitingForApprovalNotification && (groupMember.MemberStatus == GroupMemberStatus.WaitingForApproval)) { GroupMemberInfoProvider.SendNotificationMail("Groups.MemberWaitingForApproval", SiteContext.CurrentSiteName, groupMember, true); GroupMemberInfoProvider.SendNotificationMail("Groups.MemberJoinedWaitingForApproval", SiteContext.CurrentSiteName, groupMember, false); } } } // Delete all invitations to specified group for specified user (based on e-mail or userId) string whereCondition = "InvitationGroupID = " + invitation.InvitationGroupID + " AND (InvitedUserID=" + MembershipContext.AuthenticatedUser.UserID + " OR InvitationUserEmail = N'" + SqlHelper.GetSafeQueryString(MembershipContext.AuthenticatedUser.Email, false) + "')"; InvitationInfoProvider.DeleteInvitations(whereCondition); } else { lblInfo.Text = UserIsAlreadyMember.Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(group.GroupDisplayName)); lblInfo.CssClass = "InvitationErrorLabel"; // Delete this invitation InvitationInfoProvider.DeleteInvitationInfo(invitation); } } else { lblInfo.Text = GroupNoLongerExists; lblInfo.CssClass = "InvitationErrorLabel"; // Delete this invitation InvitationInfoProvider.DeleteInvitationInfo(invitation); } } else { lblInfo.Text = InvitationIsNotValid; lblInfo.CssClass = "InvitationErrorLabel"; // Delete this invitation InvitationInfoProvider.DeleteInvitationInfo(invitation); } } else { lblInfo.Text = InvitationNoLongerExists; lblInfo.CssClass = "InvitationErrorLabel"; } lblInfo.Visible = true; } else { // Hide control if invitation GUID isn't set Visible = false; } } } } }
/// <summary> /// Invite button handling. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">EventArgs</param> protected void btnInvite_Click(object sender, EventArgs e) { if (!IsLiveSite) { if (!CheckPermissions("cms.groups", CMSAdminControl.PERMISSION_MANAGE, this.GroupID)) { return; } } if (DisplayGroupSelector) { GroupID = ValidationHelper.GetInteger(groupSelector.Value, 0); } Group = GroupInfoProvider.GetGroupInfo(GroupID); string recipientEmail = string.Empty; if (Group != null) { // Check whether user is group member if (CMSContext.CurrentUser.IsGroupMember(GroupID)) { // If user can select the user if (plcUserSelector.Visible) { int userId = ValidationHelper.GetInteger(userSelector.Value, 0); if (userId > 0) { InvitedUser = UserInfoProvider.GetUserInfo(userId); bool userNotFound = true; // Check if user is filtered if (InvitedUser != null) { if (this.IsLiveSite) { userNotFound = InvitedUser.UserIsHidden || !InvitedUser.UserEnabled; } else { userNotFound = false; } } if (!userNotFound) { // Create invitation info InvitationInfo ii = InviteUser(Group, InvitedUser); if (ii != null) { // Send e-mail InvitationInfoProvider.SendInvitationEmail(ii, CMSContext.CurrentSiteName); } else { // User is member GroupMemberInfo gmi = GroupMemberInfoProvider.GetGroupMemberInfo(InvitedUser.UserID, GroupID); if (gmi != null) { lblError.Text = GetString("groupinvitation.invitationunsuccessismember").Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(Group.GroupDisplayName)).Replace("##USERNAME##", HTMLHelper.HTMLEncode(Functions.GetFormattedUserName(InvitedUser.UserName, this.IsLiveSite))); lblError.Visible = true; return; } // User is invited DataSet ds = InvitationInfoProvider.GetInvitations("InvitedUserID=" + InvitedUser.UserID + " AND InvitationGroupID=" + GroupID, "InvitationCreated", 1); if (!DataHelper.DataSourceIsEmpty(ds)) { int invitedByuserId = ValidationHelper.GetInteger(ds.Tables[0].Rows[0]["InvitedByUserID"], 0); UserInfo ui = UserInfoProvider.GetUserInfo(invitedByuserId); if (ui != null) { lblError.Text = GetString("groupinvitation.invitationunsuccesinvexists").Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(Group.GroupDisplayName)).Replace("##USERNAME##", HTMLHelper.HTMLEncode(InvitedUser.UserName)).Replace("##INVITEDBY##", HTMLHelper.HTMLEncode(Functions.GetFormattedUserName(ui.UserName, this.IsLiveSite))); lblError.Visible = true; } return; } // General error lblError.Text = GetString("groupinvitation.invitationunsuccessmult").Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(Group.GroupDisplayName)).Replace("##USERNAME##", HTMLHelper.HTMLEncode(Functions.GetFormattedUserName(InvitedUser.UserName, this.IsLiveSite))); lblError.Visible = true; return; } } else { lblError.Text = GetString("general.usernotfound"); lblError.Visible = true; return; } } else { lblError.Text = GetString("groupinvitation.emptyusers"); lblError.Visible = true; return; } // Succesfull invitation lblInfo.Text = SuccessfulInviteText; lblInfo.Visible = true; DisableAfterSuccess(); if (DisplayAdvancedOptions) { CancelButton.ResourceString = "general.back"; CancelButton.PostBackUrl = ResolveUrl("~/CMSModules/Groups/Tools/Members/Member_List.aspx?groupId=" + GroupID); } else { CancelButton.ResourceString = "general.close"; CancelButton.OnClientClick = "Close();"; } } // Single invite else { // Check the email address if it is new user if (radNewUser.Checked) { string result = new Validator().IsEmail(txtEmail.Text, rfvEmail.ErrorMessage).Result; if (result != String.Empty) { lblError.Text = result; lblError.Visible = true; return; } } InvitedUser = UserInfoProvider.GetUserInfo(InvitedUserID); if ((GroupID != 0) || (InvitedUser != null) || (radNewUser.Checked)) { // Create invitation info InvitationInfo ii = InviteUser(Group, InvitedUser); if (ii != null) { lblInfo.Text = SuccessfulInviteText; DisableAfterSuccess(); if (DisplayAdvancedOptions) { CancelButton.ResourceString = "general.back"; CancelButton.PostBackUrl = ResolveUrl("~/CMSModules/Groups/Tools/Members/Member_List.aspx?groupId=" + GroupID); } else { CancelButton.ResourceString = "general.close"; if (UseMultipleUserSelector) { CancelButton.OnClientClick = "Close();"; } else { CancelButton.OnClientClick = "CloseAndRefresh();"; } } InvitationInfoProvider.SendInvitationEmail(ii, CMSContext.CurrentSiteName); // Succesfull invitation lblInfo.Text = SuccessfulInviteText; lblInfo.Visible = true; } else { if (InvitedUser != null) { string username = HTMLHelper.HTMLEncode(Functions.GetFormattedUserName(InvitedUser.UserName, this.IsLiveSite)); // User is member GroupMemberInfo gmi = GroupMemberInfoProvider.GetGroupMemberInfo(InvitedUser.UserID, GroupID); if (gmi != null) { lblError.Text = GetString("groupinvitation.invitationunsuccessismember").Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(Group.GroupDisplayName)).Replace("##USERNAME##", username); lblError.Visible = true; return; } // User is invited DataSet ds = InvitationInfoProvider.GetInvitations("InvitedUserID=" + InvitedUser.UserID + " AND InvitationGroupID=" + GroupID, "InvitationCreated", 1); if (!DataHelper.DataSourceIsEmpty(ds)) { int invitedByuserId = ValidationHelper.GetInteger(ds.Tables[0].Rows[0]["InvitedByUserID"], 0); UserInfo ui = UserInfoProvider.GetUserInfo(invitedByuserId); if (ui != null) { lblError.Text = GetString("groupinvitation.invitationunsuccesinvexists").Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(Group.GroupDisplayName)).Replace("##USERNAME##", username).Replace("##INVITEDBY##", HTMLHelper.HTMLEncode(Functions.GetFormattedUserName(ui.UserName, this.IsLiveSite))); lblError.Visible = true; } return; } // General error lblError.Text = GetString("groupinvitation.invitationunsuccessmult").Replace("##GROUPNAME##", HTMLHelper.HTMLEncode(Group.GroupDisplayName)).Replace("##USERNAME##", username); lblError.Visible = true; return; } else { UnSuccess(); } } } else { UnSuccess(); } } } } }