예제 #1
0
파일: Aspect.cs 프로젝트: y1027/sensenet
        /// <summary>
        /// Returns an existing <see cref="Aspect"/> by the given name.
        /// If it is not found, returns null.
        /// </summary>
        /// <param name="name">Name of the <see cref="Aspect"/>.</param>
        public static Aspect LoadAspectByName(string name)
        {
            // IMPORTANT:
            // We can't use external query here because this method is called during indexing!
            // It would mean trying to query the index while writing to it, which would result in a deadlock.
            // For this reason, this method uses its own cache for finding aspects.

            // Try to find the aspect in cache
            var cacheKey = "SN_AspectCacheByName_" + name;
            var aspect   = Cache.Get(cacheKey) as Aspect;

            if (aspect == null)
            {
                // Find aspect via node query.
                // DO NOT replace this call with either Linq or Content Query for the reasons detailed above!
                var result = NodeQuery.QueryNodesByTypeAndName(ActiveSchema.NodeTypes[typeof(Aspect).Name], false, name);
                aspect = result.Nodes.FirstOrDefault() as Aspect;

                // If not found, return null
                if (aspect == null)
                {
                    return(null);
                }

                // Store in cache
                var dependency = CacheDependencyFactory.CreateNodeDependency(aspect);
                Cache.Insert(cacheKey, aspect, dependency);
            }

            return(aspect);
        }
예제 #2
0
        public override void AddPortletDependency()
        {
            base.AddPortletDependency();

            if (!CacheByContext)
            {
                return;
            }

            // this works similarly to GetCacheKey, use GetContextNodeInternal instead of GetContextNode
            Node contextNode;

            using (new SystemAccount())
            {
                contextNode = GetContextNodeInternalSafe();
            }

            if (contextNode == null)
            {
                return;
            }

            var nodeDependency = CacheDependencyFactory.CreateNodeDependency(contextNode);

            Dependencies.Add(nodeDependency);
        }
예제 #3
0
        public override void AddPortletDependency()
        {
            base.AddPortletDependency();

            if (_contentPath != null)
            {
                var nodeHead = NodeHead.Get(_contentPath);
                if (nodeHead != null)
                {
                    var nodeDependency = CacheDependencyFactory.CreateNodeDependency(nodeHead);
                    Dependencies.Add(nodeDependency);
                }
            }
        }
예제 #4
0
        public override void AddPortletDependency()
        {
            base.AddPortletDependency();

            if (!CacheByContext)
            {
                return;
            }

            //this works similar to GetCacheKey, use GetContextNodeInternal instead of GetContextNode
            var contextNode = this.GetContextNodeInternal();

            if (contextNode != null)
            {
                var nodeDependency = CacheDependencyFactory.CreateNodeDependency(contextNode);
                Dependencies.Add(nodeDependency);
            }
        }
예제 #5
0
        public static User Load(string domain, string name, ExecutionHint hint)
        {
            domain = string.IsNullOrWhiteSpace(domain) ? IdentityManagement.DefaultDomain : domain;
            if (domain == null)
            {
                throw new ArgumentNullException(nameof(domain));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            // look for the user ID in the cache by the doman-username key
            var ck           = GetUserCacheKey(domain, name);
            var userIdobject = DistributedApplication.Cache.Get(ck);

            if (userIdobject != null)
            {
                var userId     = Convert.ToInt32(userIdobject);
                var cachedUser = Node.Load <User>(userId);
                if (cachedUser != null)
                {
                    return(cachedUser);
                }
            }

            var domainPath = string.Concat(RepositoryStructure.ImsFolderPath, RepositoryPath.PathSeparator, domain);
            var type       = ActiveSchema.NodeTypes[typeof(User).Name];

            User user;
            bool forceCql;

            switch (hint)
            {
            case ExecutionHint.None:
                forceCql = RepositoryInstance.ContentQueryIsAllowed; break;

            case ExecutionHint.ForceIndexedEngine:
                forceCql = true; break;

            case ExecutionHint.ForceRelationalEngine:
                forceCql = false; break;

            default:
                throw new SnNotSupportedException("Unknown ExecutionHint: " + hint);
            }

            try
            {
                if (forceCql)
                {
                    var userResult = ContentQuery.Query(SafeQueries.UsersByLoginName, QuerySettings.AdminSettings, domainPath, name);

                    // non-unique user, do not allow login
                    if (userResult.Count > 1)
                    {
                        return(null);
                    }

                    user = userResult.Count == 0 ? null : userResult.Nodes.Cast <User>().FirstOrDefault();
                }
                else
                {
                    var queryProps = new List <QueryPropertyData>
                    {
                        new QueryPropertyData {
                            PropertyName = LOGINNAME, QueryOperator = Operator.Equal, Value = name
                        }
                    };

                    var userResult = NodeQuery.QueryNodesByTypeAndPathAndProperty(type, false, domainPath, false, queryProps);

                    // non-unique user, do not allow login
                    if (userResult.Count > 1)
                    {
                        return(null);
                    }

                    user = userResult.Count == 0 ? null : userResult.Nodes.Cast <User>().FirstOrDefault();
                }
            }
            catch (Exception e)
            {
                SnLog.WriteException(e);
                return(null);
            }

            if (user == null)
            {
                return(null);
            }

            // insert id into cache
            if (DistributedApplication.Cache.Get(ck) == null)
            {
                DistributedApplication.Cache.Insert(ck, user.Id, CacheDependencyFactory.CreateNodeDependency(user));
            }

            return(user);
        }
예제 #6
0
파일: User.cs 프로젝트: pchaozhong/FlexNet
        public static User Load(string domain, string name, ExecutionHint hint)
        {
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            //look for the user ID in the cache by the doman-username key
            var ck           = GetUserCacheKey(domain, name);
            var userIdobject = DistributedApplication.Cache.Get(ck);

            if (userIdobject != null)
            {
                var userId     = Convert.ToInt32(userIdobject);
                var cachedUser = Node.Load <User>(userId);
                if (cachedUser != null)
                {
                    //Trace.WriteLine("##UCACHE#>> User ID found in cache: " + ck);
                    return(cachedUser);
                }
            }

            //Trace.WriteLine("##UCACHE#>> User ID NOT found in cache: " + ck);

            var path = String.Concat(Repository.ImsFolderPath, RepositoryPath.PathSeparator, domain);
            var type = ActiveSchema.NodeTypes[typeof(User).Name];

            IEnumerable <Node> users;

            switch (hint)
            {
            case ExecutionHint.None:
                if (StorageContext.Search.IsOuterEngineEnabled && StorageContext.Search.SearchEngine != InternalSearchEngine.Instance)
                {
                    users = SearchInLucene(path, type.Name, name);
                }
                else
                {
                    users = NodeQuery.QueryNodesByTypeAndPathAndName(type, false, path, false, name).Nodes;
                }
                break;

            case ExecutionHint.ForceRelationalEngine:
                users = NodeQuery.QueryNodesByTypeAndPathAndName(type, false, path, false, name).Nodes;
                break;

            case ExecutionHint.ForceIndexedEngine:
                users = SearchInLucene(path, type.Name, name);
                break;

            default:
                throw new NotImplementedException();
            }

            var count = users.Count();

            if (count == 0)
            {
                return(null);
            }

            if (count > 1)
            {
                throw new ApplicationException(string.Format("The Username (='Domain\\Name', in this case '{0}\\{1}') should be unique. {2} matching users have been found.", domain, name, count));
            }

            var user = users.First() as User;

            //insert id into cache
            if (user != null && DistributedApplication.Cache.Get(ck) == null)
            {
                DistributedApplication.Cache.Insert(ck, user.Id, CacheDependencyFactory.CreateNodeDependency(user));
                //Trace.WriteLine("##UCACHE#>> User ID inserted into cache: " + ck);
            }

            return(user);
        }
예제 #7
0
파일: User.cs 프로젝트: vlslavik/SenseNet
        public static User Load(string domain, string name, ExecutionHint hint)
        {
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            //look for the user ID in the cache by the doman-username key
            var ck           = GetUserCacheKey(domain, name);
            var userIdobject = DistributedApplication.Cache.Get(ck);

            if (userIdobject != null)
            {
                var userId     = Convert.ToInt32(userIdobject);
                var cachedUser = Node.Load <User>(userId);
                if (cachedUser != null)
                {
                    return(cachedUser);
                }
            }

            var path = String.Concat(Repository.ImsFolderPath, RepositoryPath.PathSeparator, domain);
            var type = ActiveSchema.NodeTypes[typeof(User).Name];


            IEnumerable <Node> users;
            var forceCql = false;

            if (hint == ExecutionHint.None)
            {
                forceCql = StorageContext.Search.IsOuterEngineEnabled && StorageContext.Search.SearchEngine != InternalSearchEngine.Instance;
            }
            else if (hint == ExecutionHint.ForceIndexedEngine)
            {
                forceCql = true;
            }
            else if (hint == ExecutionHint.ForceRelationalEngine)
            {
                forceCql = false;
            }
            else
            {
                throw new NotImplementedException("Unknown ExecutionHint: " + hint);
            }

            try
            {
                users = forceCql
                    ? ContentQuery.Query(SafeQueries.InTreeAndTypeIsAndName, null, path, type.Name, name).Nodes
                    : users = NodeQuery.QueryNodesByTypeAndPathAndName(type, false, path, false, name).Nodes;
            }
            catch (Exception e)
            {
                Logger.WriteException(e);
                return(null);
            }

            var count = users.Count();

            if (count != 1)
            {
                return(null);
            }

            var user = users.First() as User;

            //insert id into cache
            if (user != null && DistributedApplication.Cache.Get(ck) == null)
            {
                DistributedApplication.Cache.Insert(ck, user.Id, CacheDependencyFactory.CreateNodeDependency(user));
            }

            return(user);
        }