public override async Task <ContentTemplatesReply> GetAll(Empty request, ServerCallContext context) { var reply = new ContentTemplatesReply(); var user = await _userService.GetUser(context.GetHttpContext()); if (user == null) { reply.Error = Error.NeedLogin; return(reply); } if (!user.HasWritePermission()) { reply.Error = Error.NoPermission; return(reply); } var items = _service.All(); reply.ContentTemplates.AddRange(await items .Select(i => new ContentTemplate { Id = i.Id.ToString(), Name = i.Name, Data = i.Data }) .ToArrayAsync()); return(reply); }
public override async Task <AccountReply> CheckLogin(Empty request, ServerCallContext context) { var reply = new AccountReply(); var user = await _loginUserService.GetUser(context.GetHttpContext()); if (user != null) { UpdateAccountReply(reply, user); } return(reply); }
public override async Task <KeyValuesReply> GetAll(Empty request, ServerCallContext context) { var reply = new KeyValuesReply(); var items = _service.All(); if (!(await _userService.GetUser(context.GetHttpContext())).HasWritePermission()) { items = items.Where(i => i.Type != 1); } reply.KeyValues.AddRange(await items .Select(i => new KeyValue { Id = i.Id, Value = i.Value }) .ToArrayAsync()); return(reply); }
public override async Task <UsersReply> GetAll(GetAllRequest request, ServerCallContext context) { var reply = new UsersReply(); var user = await _userService.GetUser(context.GetHttpContext()); if (user == null) { reply.Error = Error.NeedLogin; return(reply); } if (!user.HasWritePermission()) { reply.Error = Error.NoPermission; return(reply); } var users = _service.All(); if (!string.IsNullOrWhiteSpace(request.Filter)) { users = users.Where(u => u.Id.Contains(request.Filter, StringComparison.CurrentCultureIgnoreCase)); } users = users.OrderByDescending(u => u.Status).ThenBy(u => u.Id); if (request.Skip > 0 || request.Take > 0) { reply.Total = await users.CountAsync(); } if (request.Skip > 0) { users = users.Skip(request.Skip); } if (request.Take > 0) { users = users.Take(request.Take); } reply.Users.AddRange(await users .Select(Selector) .ToArrayAsync()); return(reply); }
public override async Task <TagReply> Add(Tags.Tag request, ServerCallContext context) { var reply = new TagReply(); var user = await _userService.GetUser(context.GetHttpContext()); if (user == null) { reply.Error = Error.NeedLogin; return(reply); } if (!user.HasWritePermission()) { reply.Error = Error.NoPermission; return(reply); } if (string.IsNullOrWhiteSpace(request.Name)) { reply.Error = Error.InvalidArguments; return(reply); } var existed = await _service.All().Where(i => i.Name == request.Name).FirstOrDefaultAsync(); if (existed != null) { reply.Error = Error.EntityConflict; return(reply); } var newTag = await _service.Add(new Tag { Name = request.Name, Type = (int)request.Type, Values = request.Values }); reply.Tag = Converter(newTag); return(reply); }
public override async Task <AddResponce> Add(AddRequest request, ServerCallContext context) { var reply = new AddResponce(); var user = await _userService.GetUser(context.GetHttpContext()); if (user == null) { reply.Error = Error.NeedLogin; return(reply); } if (!user.HasWritePermission()) { reply.Error = Error.NoPermission; return(reply); } var publishService = _publishServices[request.Type]; if (publishService == null) { reply.Error = Error.NoPermission; return(reply); } if (!Guid.TryParse(request.ArticleId, out var articleId)) { reply.Error = Error.NoSuchEntity; return(reply); } var file = await _filesService.All() .Where(f => f.Id == articleId && f.Type == (int)File.Types.FileType.Normal) .FirstOrDefaultAsync(); if (file == null) { reply.Error = Error.NoSuchEntity; return(reply); } var baseUrl = request.BaseUrl; try { var article = new PublishArticle { Id = file.Id, Title = file.Name, BaseUrl = baseUrl, Url = baseUrl + request.Url, Group = request.Group }; var regexp = new Regex(@"\$\{FILENAME=(.*?)\}"); var matches = regexp.Matches(request.Content); var slices = new List <PublishArticleSlice>(); var cur = 0; foreach (Match match in matches) { if (match.Index > cur) { slices.Add(new PublishArticleSlice { Type = PublishArticleSliceType.String, Value = request.Content.Substring(cur, match.Index - cur) }); } slices.Add(new PublishArticleSlice { Type = PublishArticleSliceType.Image, Value = match.Groups[1].Value }); cur = match.Index + match.Value.Length; } if (cur < request.Content.Length) { slices.Add(new PublishArticleSlice { Type = PublishArticleSliceType.String, Value = request.Content.Substring(cur, request.Content.Length - cur) }); } article.Slices = slices.ToArray(); var id = await publishService.Publish(article); reply.Id = id; } catch { reply.Error = Error.InvalidArguments; } return(reply); }
private async Task <IQueryable <File> > FilterPrivate(IQueryable <File> items, ServerCallContext context) { //todo: permission via filetype: File->Adm, Resource->Login var readPrivate = (await _userService.GetUser(context.GetHttpContext())).HasReadPrivatePermission(); if (!readPrivate) { var defStr = await _keyValueService.GetValue(ServerKeys.DefaultPrivate.Key); var defPrivate = defStr == "true"; if (defPrivate) { items = items.Where(i => i.Private == (ulong)PrivateType.Public); } else { items = items.Where(i => i.Private != (ulong)PrivateType.Private); } } return(items); }