Beispiel #1
0
 public void ChangeMessageState(int idMessage, int idState, string rememberOn)
 {
     using (Data.DataContext dataContext = SSOHelper.GetDataContext())
     {
         // Controla permisos
         var query = from r in dataContext.SSO_Messages
                     join t in dataContext.SSO_Messages_Targets on r.id equals t.idMessage
                     where r.id == idMessage &&
                     ((t.targetType == (int)SSOMessageTarget.User && t.target == SSOHelper.CurrentIdentity.Id) ||
                      (t.targetType == (int)SSOMessageTarget.Role && dataContext.SSO_UserInRole(SSOHelper.CurrentIdentity.Id, t.target) > 0))
                     select r;
         if (query.Count() > 0)
         {
             Data.SSO_Messages_States state = (from r in dataContext.SSO_Messages_States where r.idMessage == idMessage && r.idUser == SSOHelper.CurrentIdentity.Id select r).SingleOrDefault();
             if (state == null)
             {
                 state           = new Data.SSO_Messages_States();
                 state.idMessage = idMessage;
                 state.idUser    = SSOHelper.CurrentIdentity.Id;
                 dataContext.SSO_Messages_States.InsertOnSubmit(state);
             }
             state.idState    = idState;
             state.updatedOn  = DateTime.Now;
             state.rememberOn = String.IsNullOrEmpty(rememberOn) ? null : (DateTime?)DateTime.ParseExact(rememberOn, "yyyy/MM/dd", null).Date;
             dataContext.SubmitChanges();
         }
     }
 }
Beispiel #2
0
        public List <Classes.SSOMessage> GetMessages(int fromIndex, int maxRecords)
        {
            if (SSOHelper.CurrentIdentity != null)
            {
                using (Data.DataContext dataContext = SSOHelper.GetDataContext())
                {
                    List <Classes.SSOMessage> messages;
                    DateTime?timeStamp = dataContext.hsp_Common_LastUpdated("SSO_Messages_States");  /* Consulta esta tabla porque cambia más frecuentemente que SSO_Messages */
                    if (HttpContext.Current.Cache[String.Format("SSO_Messages_Timestamp_{0}", SSOHelper.CurrentIdentity.Id)] as DateTime? != timeStamp)
                    {
                        messages = (from r in dataContext.SSO_Messages
                                    join t in dataContext.SSO_Messages_Targets on r.id equals t.idMessage
                                    where (!r.expiresOn.HasValue || (r.expiresOn.HasValue && r.expiresOn >= DateTime.Now.Date)) &&
                                    r.SSO_Messages_Notifications.Count(n => n.notificationType == (int)SSOMessageNotification.Intranet) > 0 &&
                                    ((t.targetType == (int)SSOMessageTarget.User && t.target == SSOHelper.CurrentIdentity.Id) ||
                                     (t.targetType == (int)SSOMessageTarget.Role && dataContext.SSO_UserInRole(SSOHelper.CurrentIdentity.Id, t.target) > 0)) &&
                                    ((from s in dataContext.SSO_Messages_States where s.idMessage == r.id && s.idUser == SSOHelper.CurrentIdentity.Id && s.idState == (int)SSOMessageState.Sent || (s.idState == (int)SSOMessageState.RememberOn && s.rememberOn <= DateTime.Now) select s).Count() > 0)
                                    orderby r.date descending
                                    select new Classes.SSOMessage {
                            id = r.id, message = r.message, type = r.type, date = r.date
                        }).Distinct().Take(2).ToList();                                                                                                          /* Guarda en caché sólo 10. Este número debe ser siempre >= al número esperado de mensajes 'maxRecords'. */
                        HttpContext.Current.Cache[String.Format("SSO_Messages_List_{0}", SSOHelper.CurrentIdentity.Id)]      = messages;
                        HttpContext.Current.Cache[String.Format("SSO_Messages_Timestamp_{0}", SSOHelper.CurrentIdentity.Id)] = timeStamp;
                    }
                    else
                    {
                        messages = HttpContext.Current.Cache[String.Format("SSO_Messages_List_{0}", SSOHelper.CurrentIdentity.Id)] as List <Classes.SSOMessage>;
                    }

                    if (maxRecords == 0)
                    {
                        return(messages.Where(m => m.id > fromIndex).ToList());
                    }
                    else
                    {
                        return(messages.Where(m => m.id > fromIndex).Take(maxRecords).ToList());
                    }
                    //return (from m in messages where m.id > fromIndex select m).Take(maxRecords);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #3
0
        internal static SSOModule FindByURL(Uri url)
        {
            /* Here's the basic pattern:
             *  - Check the cache for the value, return if its available
             *  - If the value is not in the cache, then implement a lock
             *  - Inside the lock, check the cache again, you might have been blocked
             *  - Perform the value look up and cache it
             *  - Release the lock
             */
            string urlString = url.ToString();
            SortedDictionary <string, SSOModule> urls = SSOHelper.MembershipProvider.UseCache ? HttpContext.Current.Cache["Salud.Security.SSO.URLs"] as SortedDictionary <string, SSOModule> : null;

            if (urls != null && urls.ContainsKey(urlString))
            {
                return(urls[urlString]);
            }
            else
            {
                lock (cacheLock)
                {
                    // Busca de nuevo (ver explicación más arriba)
                    urls = SSOHelper.MembershipProvider.UseCache ? HttpContext.Current.Cache["Salud.Security.SSO.URLs"] as SortedDictionary <string, SSOModule> : null;
                    if (urls != null && urls.ContainsKey(urlString))
                    {
                        return(urls[urlString]);
                    }
                    else
                    {
                        // Busca en el caché de módulos
                        List <SSOModule> modules = SSOHelper.MembershipProvider.UseCache ? HttpContext.Current.Cache["Salud.Security.SSO.Modules"] as List <SSOModule> : null;
                        if (modules == null)
                        {
                            using (Data.DataContext DataContext = SSOHelper.GetDataContext())
                            {
                                var query = from module in DataContext.SSO_Modules
                                            join pages in DataContext.SSO_ModulePages on module.id equals pages.moduleId into joined
                                            from page in joined.DefaultIfEmpty()
                                            where module.SSO_Applications.url != null && module.SSO_Applications.url.Length > 0
                                            orderby module.SSO_Applications.url + "/" + ((page == null) ? "" : page.page) descending /* Este orden permite que primero haga el matching en las URLS XX/YY/ZZ, luego en XX/YY, luego en XX, ... */
                                            select new SSOModule(module.SSO_Applications, module.id, module.module, module.SSO_Applications.url + "/" + ((page == null) ? "" : page.page), module.name, module.description, module.@protected, module.interfase_image, module.interfase_priority, module.interfase_visible, module.groupId);
                                modules = query.ToList();
                                HttpContext.Current.Cache["Salud.Security.SSO.Modules"] = modules;
                            }
                        }

                        var result = modules.FirstOrDefault(r => r.MatchURL(url));
                        if (result == null)
                        {
                            // Busca un nivel más arriba (XX/YY/ZZ --> XX/YY)
                            string s = String.Format("{0}{1}{2}{3}", url.Scheme, Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);
                            if (s.EndsWith("/"))
                            {
                                s = s.Substring(0, s.Length - 1);
                            }
                            s = s.Substring(0, s.LastIndexOf('/'));
                            if (Uri.IsWellFormedUriString(s, UriKind.Absolute))
                            {
                                result = SSOModule.FindByURL(new Uri(s));
                            }
                        }

                        if (urls == null)
                        {
                            urls = new SortedDictionary <string, SSOModule>();
                        }
                        urls.Add(urlString, result);
                        HttpContext.Current.Cache["Salud.Security.SSO.URLs"] = urls;
                        return(result);
                    }
                }
            }
        }