private void SaveToDB(MailMessage mailMessage, List <string> fileAttachments)
        {
            EmailNotification e = new EmailNotification()
            {
                EmailNotificationId = Guid.NewGuid(),
                Subject             = mailMessage.Subject,
                Body        = mailMessage.Body,
                DateCreated = DateTime.Now
            };

            mailMessage.To.ToList().ForEach(p => e.EmailNotificationRecipients.Add(new EmailNotificationRecipient()
            {
                EmailNotificationRecipientId = Guid.NewGuid(),
                EmailNotificationId          = e.EmailNotificationId,
                RecipientEmailAddress        = p.Address,
                DateCreated = DateTime.Now
            }));
            if (fileAttachments != null)
            {
                fileAttachments.ForEach(p => e.EmailNotificationAttachments.Add(new EmailNotificationAttachment()
                {
                    EmailNotificationAttachmentId = Guid.NewGuid(),
                    EmailNotificationId           = e.EmailNotificationId,
                    AttachmentFilePath            = p,
                    DateCreated = DateTime.Now
                }));
            }
            BcEntityContext.Create().Save <EmailNotification>(e, true);
        }
 public Stream GetCountryInfos(string format)
 {
     try
     {
         ValidateRequestMethod(HttpVerb.GET);
         User u = GetCurrentUser();
         AuditServiceCall(string.Format("GetCountryCodes: {0}", u.UserName));
         List <CountryInfo> result = BcEntityContext.Create().GetCountryInfos();
         if (string.IsNullOrEmpty(format))
         {
             return(GetStreamFromObject(result));
         }
         Stream         errorStream    = null;
         SerializerType serializerType = GetSerializerTypeFromFormat(format, out errorStream);
         if (errorStream != null)
         {
             return(errorStream);
         }
         return(GetStreamFromObject(result, serializerType));
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleException(ex);
         UpdateHttpStatusOnException(ex);
         throw ex;
     }
 }
 public ActionResult Login(User model)
 {
     try
     {
         if (Request.IsAuthenticated)
         {
             return(RedirectToHome());
         }
         if (string.IsNullOrEmpty(model.UserName))
         {
             return(GetJsonResult(false, string.Format("{0} not entered.", EntityReader <User> .GetPropertyName(p => p.UserName, true))));
         }
         BcEntityContext context = BcEntityContext.Create();
         User            user    = context.Login(model.UserName, model.Password);
         if (user == null)
         {
             return(GetJsonResult(false, "Invalid user name or password."));
         }
         FormsAuthentication.SetAuthCookie(user.UserName, true);
         return(GetJsonResult(true));
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleException(ex);
         return(RedirectToError(ex.Message));
     }
 }
 public ActionResult Register(RegisterModel model)
 {
     try
     {
         string errorMessage = null;
         if (!model.IsValid(out errorMessage))
         {
             return(GetJsonResult(false, errorMessage));
         }
         User            user    = model.CreateUser(BcRole.RestApiUser);
         BcEntityContext context = BcEntityContext.Create();
         errorMessage = null;
         if (!context.RegisterUser(user, out errorMessage))
         {
             return(GetJsonResult(false, errorMessage));
         }
         FormsAuthentication.SetAuthCookie(user.UserName, true);
         return(GetJsonResult(true));
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleException(ex);
         return(RedirectToError(ex.Message));
     }
 }
 public User GetUser(string userName, BcEntityContext context, bool throwExceptionOnNotFound)
 {
     if (context == null)
     {
         context = BcEntityContext.Create();
     }
     return(context.GetUser(userName, false, throwExceptionOnNotFound));
 }
 public bool IsCurrentUserSystemAdministrator(BcEntityContext context)
 {
     if (context == null)
     {
         context = BcEntityContext.Create();
     }
     return(context.IsUserOfRole(this.User.Identity.Name, BcRole.Administrator));
 }
 public bool IsCurrentUserOfRole(BcRole roleToCheck, BcEntityContext context)
 {
     if (context == null)
     {
         context = BcEntityContext.Create();
     }
     return(context.IsUserOfRole(this.User.Identity.Name, roleToCheck));
 }
 protected bool IsCurrentUserSystemAdministrator()
 {
     if (ServiceSecurityContext.Current != null && !string.IsNullOrEmpty(ServiceSecurityContext.Current.PrimaryIdentity.Name))
     {
         string userName = ServiceSecurityContext.Current.PrimaryIdentity.Name;
         bool   result   = BcEntityContext.Create().IsUserSystemAdministrator(userName);
         return(result);
     }
     return(false);
 }
        protected User GetCurrentUser()
        {
            User result = null;

            if (ServiceSecurityContext.Current != null && !string.IsNullOrEmpty(ServiceSecurityContext.Current.PrimaryIdentity.Name))
            {
                string userName = ServiceSecurityContext.Current.PrimaryIdentity.Name;
                result = BcEntityContext.Create().GetUser(userName, false, true);
            }
            return(result);
        }
        public override void Validate(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("UserName not entered.");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("Password not entered.");
            }
            User user = BcEntityContext.Create().Login(userName, password);

            if (user == null)
            {
                throw new FaultException("Invalid UserName/Password.");
            }
        }
Exemple #11
0
        private static void ImportICalendarFiles()
        {
            BcServiceApp.Instance.Initialize(false);
            BcEntityContext context = BcEntityContext.Create();

            foreach (CountryCalendarImportInfo c in BcServiceApp.Instance.Settings.CountryCalendarImportDirectories)
            {
                if (string.IsNullOrEmpty(c.CountryCode) || string.IsNullOrEmpty(c.CountryName))
                {
                    throw new NullReferenceException(string.Format("{0} or {1} not specified on {2} in {3}.",
                                                                   EntityReader <CountryCalendarImportInfo> .GetPropertyName(p => p.CountryCode, false),
                                                                   EntityReader <CountryCalendarImportInfo> .GetPropertyName(p => p.CountryName, false),
                                                                   typeof(CountryCalendarImportInfo).Name,
                                                                   BcServiceApp.Instance.Settings.FilePath));
                }
                if (!Directory.Exists(c.ICalendarImportDirectory))
                {
                    throw new DirectoryNotFoundException(string.Format("Could not find directory {0} for country {1} ({2}).",
                                                                       c.ICalendarImportDirectory,
                                                                       c.CountryCode,
                                                                       c.CountryName));
                }
                string[] filePaths = Directory.GetFiles(c.ICalendarImportDirectory, "*.ics");
                foreach (string f in filePaths)
                {
                    GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Parsing iCalendar file: {0} ...", f), LogMessageType.Information, LoggingLevel.Normal));
                    ICalCalendar calendar = ICalPublicHolidayParser.ParseICalendarFile(f, c.CountryCode, c.CountryName);

                    GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Saving iCalendar to DB: {0} ...", f), LogMessageType.Information, LoggingLevel.Normal));
                    context.SaveICalCalendar(calendar);

                    if (BcServiceApp.Instance.Settings.DeleteICalendarFilesAfterImport)
                    {
                        GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Deleting iCalendar file: {0} ...", f), LogMessageType.Information, LoggingLevel.Normal));
                        File.Delete(f);
                    }

                    GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Completed iCalendar file import: {0} ...", f), LogMessageType.SuccessAudit, LoggingLevel.Minimum));
                }
            }
        }
Exemple #12
0
        private static void DownloadICalCalendar(string countryCode, string countryName, string startDate, string endDate)
        {
            BcServiceApp.Instance.Initialize(false);
            string downloadUrl = string.Format(BcServiceApp.Instance.Settings.ICalendarUrl, countryCode, startDate, endDate);

            using (WebClient webClient = new WebClient())
            {
                string fileName = string.Format("{0}-{1}-{2}.{3}", countryCode, startDate, endDate, ICalPublicHolidayParser.ICALENDAR_FILE_EXTENSION);
                string filePath = Path.Combine(Path.GetTempPath(), fileName);
                GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Downloading iCalendar to file: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal));
                webClient.DownloadFile(downloadUrl, filePath);

                GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Parsing iCalendar file: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal));
                ICalCalendar calender = ICalPublicHolidayParser.ParseICalendarFile(filePath, countryCode, countryName);

                BcEntityContext context = BcEntityContext.Create();
                GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Saving iCalendar to DB: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal));
                context.SaveICalCalendar(calender);

                GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Deleting iCalendar file: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal));
                File.Delete(filePath);
            }
        }
        public Stream GetPublicHolidays(string countryCode, string format, string year, string month, string day)
        {
            try
            {
                ValidateRequestMethod(HttpVerb.GET);
                User u = GetCurrentUser();
                AuditServiceCall(string.Format("GetPublicHolidays: {0}", u.UserName));

                int            yearInt;
                Nullable <int> monthInt = null;
                Nullable <int> dayInt   = null;
                if (string.IsNullOrEmpty(year) || year == "*")
                {
                    yearInt = DateTime.Now.Year;
                }
                else
                {
                    if (!int.TryParse(year, out yearInt))
                    {
                        return(StreamHelper.GetStreamFromString(string.Format(
                                                                    "Could not parse Year value '{0}' to an integer.", year),
                                                                GOC.Instance.Encoding));
                    }
                }
                if (string.IsNullOrEmpty(month) || month == "*")
                {
                    monthInt = null;
                }
                else
                {
                    int monthResult;
                    if (!int.TryParse(month, out monthResult))
                    {
                        return(StreamHelper.GetStreamFromString(string.Format(
                                                                    "Could not parse MOnth value '{0}' to an integer.", month),
                                                                GOC.Instance.Encoding));
                    }
                    monthInt = monthResult;
                }
                if (string.IsNullOrEmpty(day) || day == "*")
                {
                    dayInt = null;
                }
                else
                {
                    int dayResult;
                    if (!int.TryParse(day, out dayResult))
                    {
                        return(StreamHelper.GetStreamFromString(string.Format(
                                                                    "Could not parse MOnth value '{0}' to an integer.", day),
                                                                GOC.Instance.Encoding));
                    }
                    dayInt = dayResult;
                }
                List <PublicHolidayInfo> result = BcEntityContext.Create().GetPublicHolidayInfos(countryCode, yearInt, monthInt, dayInt);
                if (string.IsNullOrEmpty(format))
                {
                    return(GetStreamFromObject(result));
                }
                Stream         errorStream    = null;
                SerializerType serializerType = GetSerializerTypeFromFormat(format, out errorStream);
                if (errorStream != null)
                {
                    return(errorStream);
                }
                return(GetStreamFromObject(result, serializerType));
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleException(ex);
                UpdateHttpStatusOnException(ex);
                throw ex;
            }
        }