/// <summary> /// Searches the lucene index with the search text. /// </summary> /// <param name="searchText">The text to search with.</param> /// <remarks>Syntax reference: http://lucene.apache.org/java/2_3_2/queryparsersyntax.html#Wildcard</remarks> /// <exception cref="SearchException">An error occured searching the lucene.net index.</exception> public virtual IEnumerable <SearchResultViewModel> Search(string searchText) { // This check is for the benefit of the CI builds if (!Directory.Exists(IndexPath)) { CreateIndex(); } List <SearchResultViewModel> list = new List <SearchResultViewModel>(); if (string.IsNullOrWhiteSpace(searchText)) { return(list); } StandardAnalyzer analyzer = new StandardAnalyzer(LUCENEVERSION); MultiFieldQueryParser parser = new MultiFieldQueryParser(LuceneVersion.LUCENE_29, new string[] { "content", "title" }, analyzer); Query query = null; try { query = parser.Parse(searchText); } catch (Lucene.Net.QueryParsers.ParseException) { // Catch syntax errors in the search and remove them. searchText = QueryParser.Escape(searchText); query = parser.Parse(searchText); } if (query != null) { try { using (IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(new DirectoryInfo(IndexPath)), true)) { TopDocs topDocs = searcher.Search(query, 1000); foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs) { Document document = searcher.Doc(scoreDoc.Doc); list.Add(new SearchResultViewModel(document, scoreDoc)); } } } catch (FileNotFoundException) { // For 1.7's change to the Lucene search path. CreateIndex(); } catch (Exception ex) { throw new SearchException(ex, "An error occured while searching the index, try rebuilding the search index via the admin tools to fix this."); } } if (!_context.IsAdmin) { List <int> toBeRemove = new List <int>(); foreach (var page in list) { bool restrict_page = false; bool user_have_access = false; foreach (var tag1 in page.Tags.Split(' ')) { if (tag1.StartsWith("#")) { restrict_page = true; Database.User user = _UserService.GetUserById(new Guid(_context.CurrentUser)); foreach (var permission in user.Permission.Split(',')) { if (permission.ToLower() == tag1.Replace("#", "").ToLower()) { user_have_access = true; break; } } /* * if (tag1.Replace("#", "").ToLower() == _context.CurrentUsername.ToLower()) * { * user_have_access = true; * } */ } } if (restrict_page && !user_have_access) { toBeRemove.Add(page.Id); } } foreach (int i in toBeRemove) { list = list.Where(p => p.Id != i).ToList(); } } return(list); }
/// <summary> /// Finds all pages with the given tag. /// </summary> /// <param name="tag">The tag to search for.</param> /// <returns>A <see cref="IEnumerable{PageViewModel}"/> of pages tagged with the provided tag.</returns> /// <exception cref="DatabaseException">An database error occurred while getting the list.</exception> public IEnumerable <PageViewModel> FindByTag(string tag) { try { string cacheKey = string.Format("pagesbytag.{0}", tag); IEnumerable <PageViewModel> models = _listCache.Get <PageViewModel>(cacheKey); // if (models == null) //do not use cache here, so admin setting user permission will become effective immediately { IEnumerable <Page> pages = Repository.FindPagesContainingTag(tag).OrderBy(p => p.Title); if (!_context.IsAdmin) { List <int> toBeRemove = new List <int>(); foreach (var page in pages) { bool restrict_page = false; bool user_have_access = false; foreach (var tag1 in page.Tags.Split(',')) { if (tag1.StartsWith("#")) { restrict_page = true; Database.User user = _UserService.GetUserById(new Guid(_context.CurrentUser)); foreach (var permission in user.Permission.Split(',')) { if (permission.ToLower() == tag1.Replace("#", "").ToLower()) { user_have_access = true; break; } } } } if (restrict_page && !user_have_access) { toBeRemove.Add(page.Id); } } foreach (int i in toBeRemove) { pages = pages.Where(p => p.Id != i); } } models = from page in pages select new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter); _listCache.Add <PageViewModel>(cacheKey, models); } return(models); } catch (DatabaseException ex) { throw new DatabaseException(ex, "An error occurred finding the tag '{0}' in the database", tag); } }