Ejemplo n.º 1
0
        private void DeliverViaApplication(CcmsiUser user, UserReport userReport, DeliveredReportInfo info, string fileName, MemoryStream sourceStream, ScheduleRecipient recipient)
        {
            try
            {
                if (EnableFileServerSecurity)
                {
                    if (!ImpersonateUser(FileServerUserID, FileServerDomain, FileServerPassword))
                    {
                        throw new ApplicationException("Failed to impersonate user with access to file share");
                    }
                }

                var    recipientCcmsiUser = GetCcmsiUser(recipient.UserID);
                string fileFolder         = Path.Combine(FileServerPath, recipientCcmsiUser.UserID);
                if (!Directory.Exists(fileFolder))
                {
                    Directory.CreateDirectory(fileFolder);
                }

                string filePath = Path.Combine(fileFolder, fileName);

                sourceStream.Position = 0;
                using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    //read from the input stream in 4K chunks and save to file stream
                    const int BUFFER_LENGTH = 4096;
                    byte[]    buffer        = new byte[BUFFER_LENGTH];
                    int       byteCount     = 0;
                    while ((byteCount = sourceStream.Read(buffer, 0, BUFFER_LENGTH)) > 0)
                    {
                        targetStream.Write(buffer, 0, byteCount);
                    }
                    targetStream.Close();
                }

                int userReportOutputId = createUserReportOutputRecord(user, userReport, info, fileName, recipient);
                createReportDeliveryLogRecord(recipient.ScheduleID, recipient.ScheduleRecipientID, userReportOutputId);
                if (!LogExceptionsOnly)
                {
                    Logger.Write(string.Format("      Report delivered to file share. UserID = {0}, User Name = {1}, File Name = {2}",
                                               user.ID, user.UserID, filePath), "SLLog");
                }
            }
            finally
            {
                UndoImpersonation();
            }
        }
Ejemplo n.º 2
0
        private void DeliverViaEmail(string subject, string body, Schedule.Priority priority, string toEmailAddresses, DeliveredReportInfo info, string fileName, MemoryStream reportStream, ScheduleRecipient recipient, int scheduleID)
        {
            // Build the email message
            MailMessage email = new MailMessage();

            email.IsBodyHtml = true;
            email.From       = new MailAddress(SmtpFromAddress);
            email.Subject    = subject;
            email.Body       = body;
            switch (priority)
            {
            case Schedule.Priority.High:
                email.Priority = MailPriority.High;
                break;

            case Schedule.Priority.Low:
                email.Priority = MailPriority.Low;
                break;

            case Schedule.Priority.Normal:
                email.Priority = MailPriority.Normal;
                break;
            }

            reportStream.Position = 0;

            System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType(info.Extension.ToUpper() == "PDF" ? "application/pdf" : "application/vnd.ms-excel");
            contentType.Name = fileName;
            Attachment attachment = new Attachment(reportStream, contentType);

            email.Attachments.Add(attachment);

            string[] addresses = toEmailAddresses.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string address in addresses)
            {
                email.To.Add(address);
            }

            //// prepend security string
            //email.Subject = "[" + Environment.MachineName + "] " + email.Subject;

            if (IsEmailDeliveyEnabled)
            {
                SmtpClient.Send(email);
            }

            if (recipient != null)
            {
                createReportDeliveryLogRecord(scheduleID, recipient.ScheduleRecipientID, null);
            }
            else
            {
                createReportDeliveryLogRecord(scheduleID, null, null);
            }
        }
Ejemplo n.º 3
0
        public void UploadDeliveredReport(ReportUploadRequest request)
        {
            //string currentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            MemoryStream            sourceStream = null;
            DeliveredReportInfo     info         = request.DeliveredReportInfo;
            ReportProjectDBEntities entities     = new ReportProjectDBEntities();
            bool success = true;

            try
            {
                // write the byte stream to a memory stream because it can be repositioned
                const int BUFFER_LENGTH = 4096;
                byte[]    buffer        = new byte[BUFFER_LENGTH];
                int       byteCount     = 0;
                sourceStream = new MemoryStream();
                while ((byteCount = request.FileByteStream.Read(buffer, 0, BUFFER_LENGTH)) > 0)
                {
                    sourceStream.Write(buffer, 0, byteCount);
                }

                int      scheduleID = Convert.ToInt32(info.ScheduleID);
                Schedule schedule   = (from s in entities.Schedule
                                       .Include("ScheduleRecipient")
                                       where s.ScheduleID == scheduleID
                                       orderby s.ScheduleID
                                       select s).FirstOrDefault();

                int        userReportID = schedule.UserReportID;
                UserReport userReport   = (from ur in entities.UserReport.Include("ReportLayoutStyle")
                                           where ur.UserReportID == userReportID
                                           select ur).FirstOrDefault();

                if (!LogExceptionsOnly)
                {
                    Logger.Write(string.Format("**** Processing Report Delivery. Client Number = {0}, User Report = {1}({2}), ScheduleID = {3}, RS File Name = {4} ****",
                                               userReport.ClientNumber, userReport.UserReportName, userReport.UserReportID, scheduleID, info.Filename), "SLLog");
                }

                if (!IsSendReport(schedule, userReport))
                {
                    CreateReportEmptyNotSentDeliveryLog(schedule);
                }

                else
                {
                    Regex  reg      = new Regex("[^0-9a-zA-Z_]+?");
                    string fileName = reg.Replace(string.Format("{0}-{1}", userReport.UserReportName, DateTime.Now), "-");
                    fileName = string.Format("{0}.{1}", fileName, info.Extension);

                    bool isSensitive = false;

                    if (userReport.IsCustom)
                    {
                        List <UserReportField> userReportFields = (from urf in entities.UserReportField.Include("ReportField").Include("UserReport")
                                                                   where urf.UserReport.UserReportID == userReport.UserReportID
                                                                   select urf).ToList();

                        foreach (UserReportField userReportField in userReportFields)
                        {
                            if (userReportField.ReportField.IsSensitive)
                            {
                                isSensitive = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (userReport.ReportLayoutStyle.IsSensitive)
                        {
                            isSensitive = true;
                        }
                    }



                    string subject       = string.Empty;
                    var    filterService = new FilterService();

                    var userId = filterService.GetCcmsiUser(Convert.ToInt32(userReport.CreatedByUserID));

                    if (isSensitive && filterService.GetUserHasSSNAccess(userId.UserID))
                    {
                        subject = string.Format("{0}{1}", "CCMSISecure: ", schedule.Subject);
                    }
                    else
                    {
                        subject = IsReportNeedSensitive(userReport.ReportID) ? string.Format("{0}{1}", "CCMSISecure: ", schedule.Subject) : schedule.Subject;
                    }

                    foreach (ScheduleRecipient recipient in schedule.ScheduleRecipient.Where(sr => sr.IsActive).ToList())
                    {
                        CcmsiUser user = filterService.GetCcmsiUser(recipient.UserID);

                        if (recipient.DeliveryMethodType == ScheduleRecipient.DeliveryMethod.Email)
                        {
                            // deliver the email to all recipients in prod, but only to the user who created the schedule in test
                            if (IsProduction || EnabledEmailAddresses.Contains(user.EmailAddress.Trim().ToUpper())) //recipient.UserID == schedule.CreatedByUserID)
                            {
                                string emailBody          = GetEmailBody(schedule);
                                var    ccmsiDomainPattern = new Regex(@"\w+([-+.]\w+)*@ccmsi.com", RegexOptions.Compiled);
                                DeliverViaEmail(ccmsiDomainPattern.IsMatch(user.EmailAddress) ? schedule.Subject : subject, emailBody, schedule.PriorityType, user.EmailAddress, info, fileName, sourceStream, recipient, scheduleID);

                                if (!LogExceptionsOnly)
                                {
                                    Logger.Write(string.Format("      Report emailed to user. UserID = {0}, User Name = {1}, Email Address = {2}",
                                                               recipient.UserID, user.UserID, user.EmailAddress), "SLLog");
                                }
                            }
                        }
                        else
                        {
                            DeliverViaApplication(user, userReport, info, fileName, sourceStream, recipient);
                        }
                    }

                    // deliver to additional email recipients not in our database whose addresses were manually entered
                    if (IsProduction && !String.IsNullOrEmpty(schedule.AdditionalEmailAddresses) && schedule.AdditionalEmailAddresses.Trim() != String.Empty)
                    {
                        DeliverViaEmail(subject, schedule.Comment, schedule.PriorityType, schedule.AdditionalEmailAddresses.Trim(), info, fileName, sourceStream, null, scheduleID);
                        if (!LogExceptionsOnly)
                        {
                            Logger.Write(string.Format("      Report emailed to additonal recipients. Email Addresses = {0}", schedule.AdditionalEmailAddresses), "SLLog");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                success = false;
                Logger.Write(ex.ToString(), "SLLog");
                throw;
            }
            finally
            {
                UndoImpersonation();
                sourceStream.Close();

                if (!LogExceptionsOnly)
                {
                    Logger.Write(string.Format("**** Processing Report Delivery Completed. Success = {0}  ****", success), "SLLog");
                }
            }
        }
Ejemplo n.º 4
0
        private int createUserReportOutputRecord(CcmsiUser user, UserReport userReport, DeliveredReportInfo info, string filename, ScheduleRecipient recipient)
        {
            using (ReportProjectDBEntities entities = new ReportProjectDBEntities())
            {
                UserReportOutput rpt = new UserReportOutput()
                {
                    UserReport      = entities.UserReport.Where(r => r.UserReportID == userReport.UserReportID).First(),
                    UserID          = recipient.UserID,
                    FileName        = filename,
                    ClientNumber    = userReport.ClientNumber,
                    ReportRunDate   = info.ExecutionDate,
                    CreatedDate     = DateTime.Now,
                    CreatedByUserID = Convert.ToInt32(userReport.CreatedByUserID)
                };

                entities.AddToUserReportOutput(rpt);

                entities.SaveChanges();
                return(rpt.UserReportOutputID);
            }
        }