public async Task <IActionResult> SaveAsync(
            [FromForm] string id,
            [FromForm] string name,
            [FromForm] string trigger,
            [FromForm] string filter,
            [FromForm] string title,
            [FromForm] MessageBodyType bodyType,
            [FromForm] string useTemplate,
            [FromForm] string message,
            [FromForm] string tags,
            [FromForm] string[] notifiers,
            [FromForm] string[] recipients)
        {
            // Check parameter
            if (string.IsNullOrWhiteSpace(name) ||
                string.IsNullOrWhiteSpace(trigger) ||
                string.IsNullOrWhiteSpace(message))
            {
                return(BadRequest());
            }

            try
            {
                // Try save or create rule
                await _notificationRulesManager.SaveOrCreateRuleAsync(
                    id,
                    User,
                    name,
                    trigger,
                    filter,
                    title,
                    message,
                    bodyType,
                    useTemplate == "on",
                    tags,
                    notifiers,
                    recipients
                    );
            }
            catch (KeyNotFoundException kex)
            {
                return(NotFound(kex.Message));
            }
            catch (InvalidOperationException oex)
            {
                return(Forbid(oex.Message));
            }

            // Return to rules view
            return(RedirectToAction("Index", "Rules"));
        }
 /// <summary>
 /// Updates all properties with new values
 /// </summary>
 /// <param name="name">Name of rule</param>
 /// <param name="trigger">Name of trigger type</param>
 /// <param name="filter">Advaced filter</param>
 /// <param name="title">Message title</param>
 /// <param name="message">Message body</param>
 /// <param name="bodyType">Type of message body</param>
 /// <param name="useTemplate">Whether the global template should be applied</param>
 /// <param name="tags">Message tags</param>
 public void UpdateProperties(
     string name,
     string trigger,
     string filter,
     string title,
     string message,
     MessageBodyType bodyType,
     bool useTemplate,
     string tags)
 {
     DisplayName              = name;
     Trigger                  = trigger;
     AdvancedFilter           = filter;
     NotificationTitle        = title;
     NotificationBody         = message;
     NotificationBodyType     = bodyType;
     UseGlobalMessageTemplate = useTemplate;
     NotificationTags         = tags;
 }
Пример #3
0
        public TemplatedMailMessage(MessageTemplate template, Hashtable Data) : base()
        {
            string msgText = template.Transform(Data);

            XmlDocument Msg = new XmlDocument();

            Msg.LoadXml(msgText);

            this.To       = GetInnerText(Msg.SelectSingleNode("/Root/MailHeader/To"));
            this.From     = GetInnerText(Msg.SelectSingleNode("/Root/MailHeader/From"));
            this.Cc       = GetInnerText(Msg.SelectSingleNode("/Root/MailHeader/Cc"));
            this.Bcc      = GetInnerText(Msg.SelectSingleNode("/Root/MailHeader/Bcc"));
            this.Subject  = GetInnerText(Msg.SelectSingleNode("/Root/MailHeader/Subject"));
            this.Priority = (System.Net.Mail.MailPriority)Enum.Parse(typeof(System.Net.Mail.MailPriority),
                                                                     GetInnerText(Msg.SelectSingleNode("/Root/MailHeader/Priority"), "Normal"));
            XmlNode BodyNode = Msg.SelectSingleNode("/Root/Body");

            foreach (XmlNode node in BodyNode.ChildNodes)
            {
                if (node is XmlElement)
                {
                    MessageBodyType t = (MessageBodyType)Enum.Parse(typeof(MessageBodyType), FirstLetterCaps(node.Name));
                    Body.Add(t, node.InnerXml);
                }
            }
            //this.BodyHtml = XmlTools.GetOuter(Msg.SelectSingleNode("/Root/Body/html"));
            //this.BodyText = XmlTools.GetInnerText(Msg.SelectSingleNode("/Root/Body/text"));

            if (Data != null)
            {
                foreach (DictionaryEntry entry in Data)
                {
                    if (entry.Value != null)
                    {
                        this.Properties.Add(entry.Key.ToString(), entry.Value.ToString());
                    }
                }
            }
        }
Пример #4
0
 public MessageCallbackObject(IMMessage msg, MessageBodyType msgType, object call)
 {
     this.callback = call;
     this.message  = msg;
     this.msgType  = msgType;
 }
Пример #5
0
 public MessageBody(string Text, MessageBodyType BodyType)
 {
     this.Text     = Text;
     this.BodyType = BodyType;
 }
Пример #6
0
 /// <summary>
 /// Create a new instance
 /// </summary>
 /// <param name="type">Type of the message body</param>
 public Message(MessageBodyType type)
 {
     BodyType = type;
 }
Пример #7
0
        /// <summary>
        /// Trys to update a rule. Creates a new rule if not ID is supplied
        /// </summary>
        /// <param name="rule">ID of rule to change</param>
        /// <param name="owner">Current user (owner of new rule)</param>
        /// <param name="name">Name of rule</param>
        /// <param name="trigger">Trigger type</param>
        /// <param name="filter">Advanced filter</param>
        /// <param name="title">Message title</param>
        /// <param name="message">Message body</param>
        /// <param name="bodyType">Type of message body</param>
        /// <param name="useTemplate">Whether the global template should be applied</param>
        /// <param name="tags">Message tags</param>
        /// <param name="notifiers">List of assigned notifiers</param>
        /// <param name="recipients">List of assigned recipients</param>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">Thrown when rule is not found</exception>
        /// <exception cref="System.InvalidOperationException">Thrown when rule can't be edited by user</exception>
        public async Task SaveOrCreateRuleAsync(
            string id,
            ClaimsPrincipal owner,
            string name,
            string trigger,
            string filter,
            string title,
            string message,
            MessageBodyType bodyType,
            bool useTemplate,
            string tags,
            string[] notifiers,
            string[] recipients)
        {
            User currentUser = await _userManager.GetUserAsync(owner);

            if (owner == null || currentUser == null)
            {
                throw new InvalidOperationException("Current user unknown");
            }

            NotificationRule rule = await GetOrCreateRuleAsync(id, currentUser);

            if (rule == null)
            {
                throw new KeyNotFoundException("Rule not found");
            }

            // Check authorization
            if (!(await owner.IsCoordinatorAsync(_authorizationService)) &&
                rule.Owner != currentUser)
            {
                // Not coordinator nor owner
                throw new InvalidOperationException("User is not owner of this rule");
            }

            // Apply changes
            rule.UpdateProperties(name,
                                  trigger,
                                  filter,
                                  title,
                                  message,
                                  bodyType,
                                  useTemplate,
                                  tags);

            // Change notifiers
            _dbContext.RuleNotifiers.RemoveRange(rule.Notifiers);
            rule.Notifiers.Clear();
            foreach (var notifier in _dbContext.Notifiers.Where(n => notifiers.Contains(n.Id)))
            {
                rule.Notifiers.Add(new RuleNotifier(notifier));
            }

            // Change recipients
            List <string> addRecipients = new List <string>(recipients);

            // Remove recipients
            foreach (var curRecipient in rule.Recipients.ToList())
            {
                if (curRecipient.User != null)
                {
                    if (!recipients.Contains("U_" + curRecipient.User.Id))
                    {
                        _dbContext.NotificationRecipients.Remove(curRecipient);
                        rule.Recipients.Remove(curRecipient);
                    }
                    else
                    {
                        addRecipients.Remove("U_" + curRecipient.User.Id);
                    }
                }
                else
                {
                    if (!recipients.Contains("G_" + curRecipient.Role.Id))
                    {
                        _dbContext.NotificationRecipients.Remove(curRecipient);
                        rule.Recipients.Remove(curRecipient);
                    }
                    else
                    {
                        addRecipients.Remove("G_" + curRecipient.Role.Id);
                    }
                }
            }

            // Add recipients
            foreach (var addRecipient in addRecipients)
            {
                if (addRecipient.StartsWith("U"))
                {
                    var user = await _userManager.FindByIdAsync(addRecipient.Substring(2));

                    if (user != null)
                    {
                        rule.Recipients.Add(new NotificationRecipient(user));
                    }
                }
                else
                {
                    var role = await _roleManager.FindByIdAsync(addRecipient.Substring(2));

                    if (role != null)
                    {
                        rule.Recipients.Add(new NotificationRecipient(role));
                    }
                }
            }

            // Save
            await _dbContext.SaveChangesAsync();
        }