Ejemplo n.º 1
0
        /// <summary>
        /// Gets the name of the tab.
        /// </summary>
        /// <param name="tabName"></param>
        /// <param name="tabValue"></param>
        /// <returns></returns>
        private string GetTabName(string tabName, string tabValue)
        {
            switch (tabValue.ToLower())
            {
            case "tabcomments":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : IssueCommentManager.GetByIssueId(IssueId).Count));

            case "tabhistory":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : IssueHistoryManager.GetByIssueId(IssueId).Count));

            case "tabattachments":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : IssueAttachmentManager.GetByIssueId(IssueId).Count));

            case "tabnotifications":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : IssueNotificationManager.GetByIssueId(IssueId).Count));

            case "tabrelatedissues":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : RelatedIssueManager.GetRelatedIssues(IssueId).Count));

            case "tabparentissues":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : RelatedIssueManager.GetParentIssues(IssueId).Count));

            case "tabsubissues":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : RelatedIssueManager.GetChildIssues(IssueId).Count));

            case "tabrevisions":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : IssueRevisionManager.GetByIssueId(IssueId).Count));

            case "tabtimetracking":
                return(string.Format("{0} ({1})", tabName, IssueId == 0 ? 0 : IssueWorkReportManager.GetByIssueId(IssueId).Count));

            default:
                return(tabName);
            }
        }
Ejemplo n.º 2
0
        public bool CreateNewIssueAttachment(int issueId, string creatorUserName, string fileName, string contentType, byte[] attachment, int size, string description)
        {
            if (issueId <= 0)
            {
                throw new ArgumentOutOfRangeException("issueId");
            }

            var projectId = IssueManager.GetById(issueId).ProjectId;

            //authentication checks against user access to project
            if (ProjectManager.GetById(projectId).AccessType == Common.ProjectAccessType.Private && !ProjectManager.IsUserProjectMember(UserName, projectId))
            {
                throw new UnauthorizedAccessException(string.Format(LoggingManager.GetErrorMessageResource("ProjectAccessDenied"), UserName));
            }

            var issueAttachment = new IssueAttachment
            {
                Id                 = Globals.NEW_ID,
                Attachment         = attachment,
                Description        = description,
                DateCreated        = DateTime.Now,
                ContentType        = contentType,
                CreatorDisplayName = string.Empty,
                CreatorUserName    = creatorUserName,
                FileName           = fileName,
                IssueId            = issueId,
                Size               = size
            };

            return(IssueAttachmentManager.SaveOrUpdate(issueAttachment));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles the ItemDataBound event of the AttachmentsDataGrid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
        protected void AttachmentsDataGridItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

            var currentAttachment = (IssueAttachment)e.Item.DataItem;
            var lnkAttachment = e.Item.FindControl("lnkAttachment") as HtmlAnchor;

            if (lnkAttachment != null)
            {
                if (HostSettingManager.Get(HostSettingNames.AttachmentStorageType, 0) == (int)IssueAttachmentStorageTypes.FileSystem)
                {
                    lnkAttachment.InnerText = IssueAttachmentManager.StripGuidFromFileName(currentAttachment.FileName);
                }
                else
                {
                    lnkAttachment.InnerText = currentAttachment.FileName;
                }
                lnkAttachment.HRef = string.Concat("DownloadAttachment.axd?id=", currentAttachment.Id.ToString());
            }

            var lblSize = e.Item.FindControl("lblSize") as Label;

            if (lblSize == null) return;

            float size;
            string label;

            if (currentAttachment.Size > 1000)
            {
                size = currentAttachment.Size / 1000f;
                label = string.Format("{0} kb", size.ToString("##,##"));
            }
            else
            {
                size = currentAttachment.Size;
                label = string.Format("{0} b", size.ToString("##,##"));
            }

            lblSize.Text = label;

            var cmdDelete = e.Item.FindControl("cmdDelete") as ImageButton;

            // Check if the current user is Authenticated and has the permission to delete a comment			
            if (!Page.User.Identity.IsAuthenticated || !UserManager.HasPermission(ProjectId, Common.Permission.DeleteAttachment.ToString())) return;

            if (cmdDelete == null) return;

            cmdDelete.Attributes.Add("onclick", string.Format("return confirm('{0}');", GetLocalResourceObject("DeleteAttachment").ToString().Trim().JsEncode()));
            cmdDelete.Visible = false;

            // Check if it is the original user, the project admin or a super user trying to delete the comment.
            if (currentAttachment.CreatorUserName.ToLower() == Context.User.Identity.Name.ToLower() || UserManager.IsSuperUser() || UserManager.IsInRole(ProjectId, Globals.ProjectAdminRole))
            {
                cmdDelete.Visible = true;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Handles the ItemCommand event of the dtgAttachment control.
 /// </summary>
 /// <param name="source">The source of the event.</param>
 /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param>
 protected void AttachmentsDataGridItemCommand(object source, DataGridCommandEventArgs e)
 {
     switch (e.CommandName)
     {
         case "Delete":
             IssueAttachmentManager.Delete(Convert.ToInt32(e.CommandArgument));
             break;
     }
     BindAttachments();
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the name of the tab.
        /// </summary>
        /// <param name="tabName"></param>
        /// <param name="tabValue"></param>
        /// <returns></returns>
        private string GetTabName(string tabName, string tabValue)
        {
            int cnt;

            switch (tabValue.ToLower())
            {
            case "tabcomments":
                cnt = IssueId == 0 ? 0 : IssueCommentManager.GetByIssueId(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "bold"));

            case "tabhistory":
                cnt = IssueId == 0 ? 0 : IssueHistoryManager.GetByIssueId(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "normal"));

            case "tabattachments":
                cnt = IssueId == 0 ? 0 : IssueAttachmentManager.GetByIssueId(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "bold"));

            case "tabnotifications":
                cnt = IssueId == 0 ? 0 : IssueNotificationManager.GetByIssueId(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "normal"));

            case "tabrelatedissues":
                cnt = IssueId == 0 ? 0 : RelatedIssueManager.GetRelatedIssues(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "bold"));

            case "tabparentissues":
                cnt = IssueId == 0 ? 0 : RelatedIssueManager.GetParentIssues(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "bold"));

            case "tabsubissues":
                cnt = IssueId == 0 ? 0 : RelatedIssueManager.GetChildIssues(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "bold"));

            case "tabrevisions":
                cnt = IssueId == 0 ? 0 : IssueRevisionManager.GetByIssueId(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "normal"));

            case "tabtimetracking":
                cnt = IssueId == 0 ? 0 : IssueWorkReportManager.GetByIssueId(IssueId).Count;
                return(string.Format("<span class='{2}'>{0} ({1})</span>", tabName, cnt, cnt == 0 ? "normal" : "normal"));

            default:
                return(tabName);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Binds the attachments.
        /// </summary>
        private void BindAttachments()
        {
            //Fix tab names after adding or deleting a record.
           //IssueTabs tabs = this.Parent as Issues.UserControls.IssueTabs;
           //tabs.RefreshTabNames();
            List<IssueAttachment> attachments = IssueAttachmentManager.GetByIssueId(IssueId);

            AttachmentDescription.Text = string.Empty;

            if (attachments.Count == 0)
            {
                lblAttachments.Text = GetLocalResourceObject("NoAttachments").ToString();
                lblAttachments.Visible = true;
                AttachmentsDataGrid.Visible = false;
            }
            else
            {
                lblAttachments.Visible = false;
                AttachmentsDataGrid.DataSource = attachments;
                AttachmentsDataGrid.DataBind();
                AttachmentsDataGrid.Visible = true;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Saves the mailbox entry.
        /// </summary>
        /// <param name="entry">The entry.</param>
        void SaveMailboxEntry(MailboxEntry entry)
        {
            try
            {
                //load template
                var body = string.Format("<div >Sent by:{1} on: {2}<br/>{0}</div>", entry.Content.Trim(), entry.From, entry.Date);

                if (Config.BodyTemplate.Trim().Length > 0)
                {
                    var data = new Dictionary <string, object> {
                        { "MailboxEntry", entry }
                    };
                    body = NotificationManager.GenerateNotificationContent(Config.BodyTemplate, data);
                }

                var projectId = entry.ProjectMailbox.ProjectId;

                var mailIssue = IssueManager.GetDefaultIssueByProjectId(
                    projectId,
                    entry.Title.Trim(),
                    body.Trim(),
                    entry.ProjectMailbox.AssignToUserName,
                    Config.ReportingUserName);

                if (!IssueManager.SaveOrUpdate(mailIssue))
                {
                    return;
                }

                entry.IssueId      = mailIssue.Id;
                entry.WasProcessed = true;

                var project = ProjectManager.GetById(projectId);

                var projectFolderPath = Path.Combine(Config.UploadsFolderPath, project.UploadPath);

                var doc = new HtmlDocument();
                doc.LoadHtml(mailIssue.Description); // load the issue body to we can process it for inline images (if exist)

                //If there is an attached file present then add it to the database
                //and copy it to the directory specified in the web.config file
                foreach (MIME_Entity mimeEntity in entry.MailAttachments)
                {
                    string fileName;
                    var    isInline    = false;
                    var    contentType = mimeEntity.ContentType.Type.ToLower();

                    var attachment = new IssueAttachment
                    {
                        Id                 = 0,
                        Description        = "File attached by mailbox reader",
                        DateCreated        = DateTime.Now,
                        ContentType        = mimeEntity.ContentType.TypeWithSubtype,
                        CreatorDisplayName = Config.ReportingUserName,
                        CreatorUserName    = Config.ReportingUserName,
                        IssueId            = mailIssue.Id,
                        ProjectFolderPath  = projectFolderPath
                    };

                    switch (contentType)
                    {
                    case "application":
                        attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;
                        break;

                    case "attachment":
                    case "image":
                    case "video":
                    case "audio":

                        attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;
                        break;

                    case "message":

                        // we need to pull the actual email message out of the entity, and strip the "content type" out so that
                        // email programs will read the file properly
                        var messageBody = mimeEntity.ToString().Replace(mimeEntity.Header.ToString(), "");
                        if (messageBody.StartsWith("\r\n"))
                        {
                            messageBody = messageBody.Substring(2);
                        }

                        attachment.Attachment = Encoding.UTF8.GetBytes(messageBody);

                        break;

                    default:
                        LogWarning(string.Format("MailboxReader: Attachment type could not be processed {0}", mimeEntity.ContentType.Type));
                        break;
                    }

                    if (contentType.Equals("attachment")) // this is an attached email
                    {
                        fileName = mimeEntity.ContentDisposition.Param_FileName;
                    }
                    else if (contentType.Equals("message")) // message has no filename so we create one
                    {
                        fileName = string.Format("Attached_Message_{0}.eml", entry.AttachmentsSavedCount);
                    }
                    else
                    {
                        isInline = true;
                        fileName = string.IsNullOrWhiteSpace(mimeEntity.ContentType.Param_Name) ?
                                   string.Format("untitled.{0}", mimeEntity.ContentType.SubType) :
                                   mimeEntity.ContentType.Param_Name;
                    }

                    attachment.FileName = fileName;

                    var saveFile  = IsAllowedFileExtension(fileName);
                    var fileSaved = false;

                    // can we save the file?
                    if (saveFile)
                    {
                        fileSaved = IssueAttachmentManager.SaveOrUpdate(attachment);

                        if (fileSaved)
                        {
                            entry.AttachmentsSavedCount++;
                        }
                        else
                        {
                            LogWarning("MailboxReader: Attachment could not be saved, please see previous logs");
                        }
                    }

                    if (!entry.IsHtml || !isInline)
                    {
                        continue;
                    }

                    if (string.IsNullOrWhiteSpace(mimeEntity.ContentID))
                    {
                        continue;
                    }

                    var contentId = mimeEntity.ContentID.Replace("<", "").Replace(">", "").Replace("[", "").Replace("]", "");

                    // this is pretty greedy but since people might be sending screenshots I doubt they will send in dozens of images
                    // embedded in the email.  one would hope
                    foreach (var node in doc.DocumentNode.SelectNodes(XpathElementCaseInsensitive("img")).ToList())
                    {
                        var attr = node.Attributes.FirstOrDefault(p => p.Name.ToLowerInvariant() == "src");// get the src attribute

                        if (attr == null)
                        {
                            continue;               // image has no src attribute
                        }
                        if (!attr.Value.Contains(contentId))
                        {
                            continue;                                  // is the attribute value the content id?
                        }
                        // swap out the content of the parent node html will our link to the image
                        var anchor = string.Format("<span class='inline-mail-attachment'>Inline Attachment: <a href='DownloadAttachment.axd?id={0}' target='_blank'>{1}</a></span>", attachment.Id, fileName);

                        // for each image in the body if the file was saved swap out the inline link for a link to the saved attachment
                        // otherwise blank out the content link so we don't get a missing image link
                        node.ParentNode.InnerHtml = fileSaved ? anchor : "";
                    }

                    mailIssue.Description        = doc.DocumentNode.InnerHtml;
                    mailIssue.LastUpdateUserName = mailIssue.OwnerUserName;
                    mailIssue.LastUpdate         = DateTime.Now;

                    IssueManager.SaveOrUpdate(mailIssue);
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
                throw;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["mode"] == "project")
            {
                var projectId = context.Request.QueryString.Get("id", Globals.NEW_ID);

                var projectImage = ProjectManager.GetProjectImageById(projectId);

                if (projectImage != null)
                {
                    // Write out the attachment
                    context.Server.ScriptTimeout = 600;
                    context.Response.Buffer      = true;
                    context.Response.Clear();
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.AddHeader("Content-Length", projectImage.ImageFileLength.ToString());
                    context.Response.BinaryWrite(projectImage.ImageContent);
                }
                else
                {
                    context.Response.WriteFile("~/Images/noimage.gif");
                }
            }
            else
            {
                // Get the attachment
                var attachmentId = context.Request.Get("id", Globals.NEW_ID);

                // cannot parse the attachment from the querystring bail without trying
                if (attachmentId.Equals(Globals.NEW_ID))
                {
                    context.Response.Write("<h1>Attachment Not Found.</h1>  It may have been deleted from the server.");
                    context.Response.End();
                    return;
                }

                try
                {
                    var attachment = IssueAttachmentManager.GetAttachmentForDownload(attachmentId);

                    if (attachment == null)
                    {
                        context.Response.Write("<h1>Attachment Not Found.</h1>  It may have been deleted from the server.");
                        context.Response.End();
                        return;
                    }

                    var cleanFileName = IssueAttachmentManager.StripGuidFromFileName(attachment.FileName);
                    var fileName      = attachment.FileName;

                    if (attachment.Attachment != null)
                    {
                        // Write out the attachment
                        context.Server.ScriptTimeout = 600;
                        context.Response.Buffer      = true;
                        context.Response.Clear();

                        if (attachment.ContentType.ToLower().StartsWith("image/"))
                        {
                            context.Response.ContentType = attachment.ContentType;
                            context.Response.AddHeader("Content-Disposition", string.Format("inline; filename=\"{0}\";", fileName));
                        }
                        else
                        {
                            context.Response.ContentType = "application/octet-stream";
                            context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\";", fileName));
                        }
                        context.Response.AddHeader("Content-Length", attachment.Attachment.Length.ToString());
                        context.Response.BinaryWrite(attachment.Attachment);
                    }
                    else
                    {
                        var p = ProjectManager.GetById(IssueManager.GetById(attachment.IssueId).ProjectId);
                        if (string.IsNullOrEmpty(p.UploadPath))
                        {
                            p.UploadPath = p.Id.ToString();//use project id as pathroot
                        }
                        var projectPath = p.UploadPath;
                        // append a trailing slash if it doesn't exist
                        if (!projectPath.EndsWith(@"\"))
                        {
                            projectPath = String.Concat(projectPath, @"\");
                        }

                        var path = String.Concat(HostSettingManager.Get(HostSettingNames.AttachmentUploadPath), projectPath, fileName);

                        if (HostSettingManager.Get(HostSettingNames.AttachmentUploadPath).StartsWith("~"))
                        {
                            path = context.Server.MapPath(path);
                        }

                        if (System.IO.File.Exists(path))
                        {
                            context.Response.Clear();
                            context.Response.ContentType = attachment.ContentType;
                            context.Response.AddHeader("Content-Disposition",
                                                       attachment.ContentType.ToLower().StartsWith("image/")
                                                           ? string.Format("inline; filename=\"{0}\";", cleanFileName)
                                                           : string.Format("attachment; filename=\"{0}\";",
                                                                           cleanFileName));
                            context.Response.WriteFile(path);
                        }
                        else
                        {
                            context.Response.Write("<h1>Attachment Not Found.</h1>  It may have been deleted from the server.");
                        }
                    }
                }
                catch (DataAccessException dx)
                {
                    if (dx.StatusCode > 0)
                    {
                        var statusCode = dx.StatusCode.ToEnum(DownloadAttachmentStatusCodes.NoAccess);

                        var url       = context.Request.Url.PathAndQuery.Trim().ToLower();
                        var fullPath  = context.Request.Url.ToString().ToLower();
                        var authority = fullPath.Replace(url, "");

                        var redirectUrl =
                            string.Format("~/Account/Login.aspx?ReturnUrl={0}{1}", authority, context.Server.UrlEncode(url));

                        switch (statusCode)
                        {
                        case DownloadAttachmentStatusCodes.InvalidAttachmentId:
                            context.Response.Write("<h1>Attachment Not Found.</h1>  It may have been deleted from the server.");
                            break;

                        case DownloadAttachmentStatusCodes.AuthenticationRequired:
                            context.Response.Redirect(redirectUrl);
                            break;

                        case DownloadAttachmentStatusCodes.ProjectOrIssueDisabled:
                            context.Response.Write("<h1>Attachment Not Found.</h1>  It may have been deleted from the server.");
                            break;

                        case DownloadAttachmentStatusCodes.NoAccess:
                            context.Response.Write("<h1>Access Denied.</h1>  You do not have proper permissions to access this Attachment.");
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }
            }

            // End the response
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Uploads the document.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void UploadDocument(object sender, EventArgs e)
        {
            // get the current file
            var uploadFile = AspUploadFile.PostedFile;

            // if there was a file uploaded
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                var inValidReason = string.Empty;
                var fileName = Path.GetFileName(uploadFile.FileName);

                var validFile = IssueAttachmentManager.IsValidFile(fileName, out inValidReason);

                if (validFile)
                {
                    byte[] fileBytes;
                    using (var input = uploadFile.InputStream)
                    {
                        fileBytes = new byte[uploadFile.ContentLength];
                        input.Read(fileBytes, 0, uploadFile.ContentLength);
                    }

                    var attachment = new IssueAttachment
                    {
                        Id = Globals.NEW_ID,
                        Attachment = fileBytes,
                        Description = AttachmentDescription.Text.Trim(),
                        DateCreated = DateTime.Now,
                        ContentType = uploadFile.ContentType,
                        CreatorDisplayName = string.Empty,
                        CreatorUserName = Security.GetUserName(),
                        FileName = fileName,
                        IssueId = IssueId,
                        Size = fileBytes.Length
                    };

                    if (!IssueAttachmentManager.SaveOrUpdate(attachment))
                    {
                        AttachmentsMessage.ShowErrorMessage(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        if (Log.IsWarnEnabled) Log.Warn(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        return;
                    }

                    //add history record and send notifications
                    var history = new IssueHistory
                    {
                        IssueId = IssueId,
                        CreatedUserName = Security.GetUserName(),
                        DateChanged = DateTime.Now,
                        FieldChanged = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Attachment", "Attachment"),
                        OldValue = fileName,
                        NewValue = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Added", "Added"),
                        TriggerLastUpdateChange = true
                    };

                    IssueHistoryManager.SaveOrUpdate(history);

                    var changes = new List<IssueHistory> { history };

                    IssueNotificationManager.SendIssueNotifications(IssueId, changes);

                    BindAttachments();
                }
                else
                    AttachmentsMessage.ShowErrorMessage(inValidReason); 
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Saves the issue.
        /// </summary>
        /// <returns></returns>
        private bool SaveIssue()
        {
            decimal estimation;

            decimal.TryParse(txtEstimation.Text.Trim(), out estimation);
            var dueDate = DueDatePicker.SelectedValue ?? DateTime.MinValue;

            var issue = new Issue
            {
                AffectedMilestoneId       = DropAffectedMilestone.SelectedValue,
                AffectedMilestoneImageUrl = string.Empty,
                AffectedMilestoneName     = DropAffectedMilestone.SelectedText,
                AssignedDisplayName       = DropAssignedTo.SelectedText,
                AssignedUserId            = Guid.Empty,
                AssignedUserName          = DropAssignedTo.SelectedValue,
                CategoryId         = DropCategory.SelectedValue,
                CategoryName       = DropCategory.SelectedText,
                CreatorDisplayName = Security.GetDisplayName(),
                CreatorUserId      = Guid.Empty,
                CreatorUserName    = Security.GetUserName(),
                DateCreated        = DateTime.Now,
                Description        = DescriptionHtmlEditor.Text.Trim(),
                Disabled           = false,
                DueDate            = dueDate,
                Estimation         = estimation,
                Id                    = 0,
                IsClosed              = false,
                IssueTypeId           = DropIssueType.SelectedValue,
                IssueTypeName         = DropIssueType.SelectedText,
                IssueTypeImageUrl     = string.Empty,
                LastUpdate            = DateTime.Now,
                LastUpdateDisplayName = Security.GetDisplayName(),
                LastUpdateUserName    = Security.GetUserName(),
                MilestoneDueDate      = null,
                MilestoneId           = DropMilestone.SelectedValue,
                MilestoneImageUrl     = string.Empty,
                MilestoneName         = DropMilestone.SelectedText,
                OwnerDisplayName      = DropOwned.SelectedText,
                OwnerUserId           = Guid.Empty,
                OwnerUserName         = DropOwned.SelectedValue,
                PriorityId            = DropPriority.SelectedValue,
                PriorityImageUrl      = string.Empty,
                PriorityName          = DropPriority.SelectedText,
                Progress              = Convert.ToInt32(ProgressSlider.Text),
                ProjectCode           = string.Empty,
                ProjectId             = ProjectId,
                ProjectName           = string.Empty,
                ResolutionId          = DropResolution.SelectedValue,
                ResolutionImageUrl    = string.Empty,
                ResolutionName        = DropResolution.SelectedText,
                StatusId              = DropStatus.SelectedValue,
                StatusImageUrl        = string.Empty,
                StatusName            = DropStatus.SelectedText,
                Title                 = Server.HtmlEncode(TitleTextBox.Text),
                TimeLogged            = 0,
                Visibility            = chkPrivate.Checked ? 1 : 0,
                Votes                 = 0
            };

            if (!IssueManager.SaveOrUpdate(issue))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueError);
                return(false);
            }

            if (!CustomFieldManager.SaveCustomFieldValues(issue.Id, ctlCustomFields.Values, true))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveCustomFieldValuesError);
                return(false);
            }

            IssueId = issue.Id;

            //add attachment if present.
            if (AspUploadFile.HasFile)
            {
                // get the current file
                var    uploadFile = AspUploadFile.PostedFile;
                string inValidReason;
                var    validFile = IssueAttachmentManager.IsValidFile(uploadFile.FileName, out inValidReason);

                if (validFile)
                {
                    if (uploadFile.ContentLength > 0)
                    {
                        byte[] fileBytes;
                        using (var input = uploadFile.InputStream)
                        {
                            fileBytes = new byte[uploadFile.ContentLength];
                            input.Read(fileBytes, 0, uploadFile.ContentLength);
                        }

                        var issueAttachment = new IssueAttachment
                        {
                            Id                 = Globals.NEW_ID,
                            Attachment         = fileBytes,
                            Description        = AttachmentDescription.Text.Trim(),
                            DateCreated        = DateTime.Now,
                            ContentType        = uploadFile.ContentType,
                            CreatorDisplayName = string.Empty,
                            CreatorUserName    = Security.GetUserName(),
                            FileName           = uploadFile.FileName,
                            IssueId            = issue.Id,
                            Size               = fileBytes.Length
                        };

                        if (!IssueAttachmentManager.SaveOrUpdate(issueAttachment))
                        {
                            Message1.ShowErrorMessage(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        }
                    }
                }
                else
                {
                    Message1.ShowErrorMessage(inValidReason);
                    return(false);
                }
            }

            //create a vote for the new issue
            var vote = new IssueVote {
                IssueId = issue.Id, VoteUsername = Security.GetUserName()
            };

            if (!IssueVoteManager.SaveOrUpdate(vote))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueVoteError);
                return(false);
            }

            if (chkNotifyOwner.Checked && !string.IsNullOrEmpty(issue.OwnerUserName))
            {
                var oUser = UserManager.GetUser(issue.OwnerUserName);
                if (oUser != null)
                {
                    var notify = new IssueNotification {
                        IssueId = issue.Id, NotificationUsername = oUser.UserName
                    };
                    IssueNotificationManager.SaveOrUpdate(notify);
                }
            }
            if (chkNotifyAssignedTo.Checked && !string.IsNullOrEmpty(issue.AssignedUserName))
            {
                var oUser = UserManager.GetUser(issue.AssignedUserName);
                if (oUser != null)
                {
                    var notify = new IssueNotification {
                        IssueId = issue.Id, NotificationUsername = oUser.UserName
                    };
                    IssueNotificationManager.SaveOrUpdate(notify);
                }
            }

            //send issue notifications
            IssueNotificationManager.SendIssueAddNotifications(issue.Id);

            return(true);
        }
Ejemplo n.º 11
0
        private bool ProcessNewComment(List <string> recipients, POP3_ClientMessage message, Mail_Message mailHeader, MailboxReaderResult result)
        {
            string messageFrom = string.Empty;

            if (mailHeader.From.Count > 0)
            {
                messageFrom = string.Join("; ", mailHeader.From.ToList().Select(p => p.Address).ToArray()).Trim();
            }

            bool processed = false;

            foreach (var address in recipients)
            {
                Regex isReply      = new Regex(@"(.*)(\+iid-)(\d+)@(.*)");
                Match commentMatch = isReply.Match(address);
                if (commentMatch.Success && commentMatch.Groups.Count >= 4)
                {
                    // we are in a reply and group 4 must contain the id of the original issue
                    int issueId;
                    if (int.TryParse(commentMatch.Groups[3].Value, out issueId))
                    {
                        var _currentIssue = IssueManager.GetById(issueId);

                        if (_currentIssue != null)
                        {
                            var project = ProjectManager.GetById(_currentIssue.ProjectId);

                            var mailbody = Mail_Message.ParseFromByte(message.MessageToByte());

                            bool isHtml;
                            List <MIME_Entity> attachments = null;
                            string             content     = GetMessageContent(mailbody, project, out isHtml, ref attachments);

                            IssueComment comment = new IssueComment
                            {
                                IssueId     = issueId,
                                Comment     = content,
                                DateCreated = mailHeader.Date
                            };

                            // try to find if the creator is valid user in the project, otherwise take
                            // the user defined in the mailbox config
                            var users  = UserManager.GetUsersByProjectId(project.Id);
                            var emails = messageFrom.Split(';').Select(e => e.Trim().ToLower());
                            var user   = users.Find(x => emails.Contains(x.Email.ToLower()));
                            if (user != null)
                            {
                                comment.CreatorUserName = user.UserName;
                            }
                            else
                            {
                                // user not found
                                continue;
                            }

                            var saved = IssueCommentManager.SaveOrUpdate(comment);
                            if (saved)
                            {
                                //add history record
                                var history = new IssueHistory
                                {
                                    IssueId                 = issueId,
                                    CreatedUserName         = comment.CreatorUserName,
                                    DateChanged             = comment.DateCreated,
                                    FieldChanged            = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Comment", "Comment"),
                                    OldValue                = string.Empty,
                                    NewValue                = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Added", "Added"),
                                    TriggerLastUpdateChange = true
                                };
                                IssueHistoryManager.SaveOrUpdate(history);

                                var projectFolderPath = Path.Combine(Config.UploadsFolderPath, project.UploadPath);

                                // save attachments as new files
                                int attachmentsSavedCount = 1;
                                foreach (MIME_Entity mimeEntity in attachments)
                                {
                                    string fileName;
                                    var    contentType = mimeEntity.ContentType.Type.ToLower();

                                    var attachment = new IssueAttachment
                                    {
                                        Id                 = 0,
                                        Description        = "File attached by mailbox reader",
                                        DateCreated        = DateTime.Now,
                                        ContentType        = mimeEntity.ContentType.TypeWithSubtype,
                                        CreatorDisplayName = user.DisplayName,
                                        CreatorUserName    = user.UserName,
                                        IssueId            = issueId,
                                        ProjectFolderPath  = projectFolderPath
                                    };
                                    attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;

                                    if (contentType.Equals("attachment")) // this is an attached email
                                    {
                                        fileName = mimeEntity.ContentDisposition.Param_FileName;
                                    }
                                    else if (contentType.Equals("message")) // message has no filename so we create one
                                    {
                                        fileName = string.Format("Attached_Message_{0}.eml", attachmentsSavedCount);
                                    }
                                    else
                                    {
                                        fileName = string.IsNullOrWhiteSpace(mimeEntity.ContentType.Param_Name) ?
                                                   string.Format("untitled.{0}", mimeEntity.ContentType.SubType) :
                                                   mimeEntity.ContentType.Param_Name;
                                    }

                                    attachment.FileName = fileName;

                                    var saveFile  = IsAllowedFileExtension(fileName);
                                    var fileSaved = false;

                                    // can we save the file?
                                    if (saveFile)
                                    {
                                        fileSaved = IssueAttachmentManager.SaveOrUpdate(attachment);

                                        if (fileSaved)
                                        {
                                            attachmentsSavedCount++;
                                        }
                                        else
                                        {
                                            LogWarning("MailboxReader: Attachment could not be saved, please see previous logs");
                                        }
                                    }
                                }

                                processed = true;

                                // add the entry if the save did not throw any exceptions
                                result.MailboxEntries.Add(new MailboxEntry());
                            }
                        }
                    }
                }
            }
            return(processed);
        }