Esempio n. 1
0
        public async Task <NotificationApplication> GetNotificationApplication(Guid notificationId)
        {
            //TODO: Remove this method and replace usages with repositories

            var notification = await NotificationApplications.SingleAsync(n => n.Id == notificationId);

            if (!(await IsInternal()) && notification.UserId != UserContext.UserId && !(await IsSharedUser(notificationId)))
            {
                throw new SecurityException(string.Format("Access denied to this notification {0} for user {1}",
                                                          notificationId, UserContext.UserId));
            }
            return(notification);
        }
Esempio n. 2
0
        public static void SendNotification(NotificationTypes notificationType, NotificationApplications application, System.String eventCode,
                                            params System.Object[] parameters)
        {
            IList <Recipient> recipients = Service.GetRecipients(eventCode);

            if (recipients.Count == 0)
            {
                return;
            }

            IDictionary <System.Int32, System.String> eventStrings = Service.GetEventStrings(application, eventCode);

            foreach (Recipient recipient in recipients)
            {
                if (recipient.EMail != "*****@*****.**")
                {
                    continue;
                }

                System.String text;
                if (eventStrings.ContainsKey(recipient.LanguageId))
                {
                    text = eventStrings[recipient.LanguageId];
                }
                else if (eventStrings.ContainsKey(1))
                {
                    text = eventStrings[1];
                }
                else
                {
                    text = eventCode;
                }

                text = System.String.Format(text, parameters);

                try
                {
                    new Thread(() => EmailService.PreparationDataForSending(recipient, notificationType, text, application, eventCode)).Start();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"SendNotification error - {ex}");
                }
            }
        }
Esempio n. 3
0
        public static IDictionary <Int32, String> GetEventStrings(NotificationApplications application, String eventCode)
        {
            IDictionary <Int32, String> eventStrings = new Dictionary <Int32, String>();
            SqlParameter appIdParam   = CreateParameter("@application_id", SqlDbType.Int, (Int32)application);
            SqlParameter codeAppParam = CreateParameter("@code_app", SqlDbType.NVarChar, eventCode);

            using (SqlConnection connection = new SqlConnection(CONN_STRING))
            {
                SqlCommand command = new SqlCommand(Queries.GET_EVENT_STRINGS, connection);

                command.Parameters.Add(appIdParam);
                command.Parameters.Add(codeAppParam);

                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    try
                    {
                        while (reader.Read())
                        {
                            Int32  languageId = (Int32)reader["language_id"];
                            String text       = (String)reader["text"];

                            if (!eventStrings.ContainsKey(languageId))
                            {
                                eventStrings.Add(languageId, text);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Exception GetEventStrings - {ex}");
                    }
                }
                return(eventStrings);
            }
        }
Esempio n. 4
0
        public static void PreparationDataForSending(Recipient recipient, NotificationTypes notificationType, String text, NotificationApplications application, String eventCode)
        {
            log.Log.Info($"{recipient.EMail}, {notificationType.ToString()} {application.ToString()}, {eventCode}");

            if (string.IsNullOrEmpty(recipient.EMail))
            {
                log.Log.Warn("EMail is empty");
                return;
            }

            Random rnd = new Random();

            EmailData emailData = new EmailData()
            {
                R1      = rnd.Next(1000000, 9999999),
                Sender  = recipient.EMail,
                Subject = GetSubject((Int32)application, eventCode),
                Text    = text,
                R2      = rnd.Next(1000000, 9999999)
            };

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = true
            };

            String finalText = string.Empty;

            try
            {
                /*
                 * We convert the emailData object into JSON, JSON into an array of bytes,
                 * and already this array of bytes is passed to the EncryptStringToBytes_Aes function.
                 */


                byte[] encryptedByte = AesEncryption.EncryptStringToBytes_Aes(JsonSerializer.SerializeToUtf8Bytes(emailData, options));
                finalText = Convert.ToBase64String(encryptedByte);
            }
            catch (Exception ex)
            {
                log.Log.Error(ex, $"Failed to use AES encryption. Message - {ex}");
                return;
            }

            Task <String> tasks = RequestSendingEmail(finalText);

            Int32?code = ProcessingResult(tasks.Result);

            if (code != null)
            {
                try
                {
                    // Add the response to the current request for sending to the log table
                    // If the code is 1 (successful), then add the current notification to the table with the "sent" flag.
                    // This is necessary so as not to break the current connectivity of notification events, since many
                    // notifications are checked for the sending frequency from this table.
                    Service.WriteResponseToLogNtfTable(recipient.Id, (Int32)notificationType, text, (Int32)code, application.ToString());
                }
                catch (Exception ex)
                {
                    log.Log.Error(ex, "Failed to write result to table.");
                }
            }
        }