Esempio n. 1
0
        public IHttpActionResult Delete(int id)
        {
            try
            {
                var request         = new AuthenticatedRequest();
                var isApiAuthorized = request.IsApiAuthenticated && AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeAdministrators);
                if (!isApiAuthorized)
                {
                    return(Unauthorized());
                }

                if (!DataProvider.AdministratorDao.ApiIsExists(id))
                {
                    return(NotFound());
                }

                var adminInfo = DataProvider.AdministratorDao.ApiDelete(id);

                return(Ok(new
                {
                    Value = adminInfo
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 2
0
        public IHttpActionResult List()
        {
            try
            {
                var request         = new AuthenticatedRequest();
                var isApiAuthorized = request.IsApiAuthenticated && AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeAdministrators);
                if (!isApiAuthorized)
                {
                    return(Unauthorized());
                }

                var top  = request.GetQueryInt("top", 20);
                var skip = request.GetQueryInt("skip");

                var administrators = DataProvider.AdministratorDao.ApiGetAdministrators(skip, top);
                var count          = DataProvider.AdministratorDao.ApiGetCount();

                return(Ok(new PageResponse(administrators, top, skip, request.HttpRequest.Url.AbsoluteUri)
                {
                    Count = count
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 3
0
        public IHttpActionResult Get(int id)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeUsers) ||
                              request.IsUserLoggin &&
                              request.UserId == id ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasSystemPermissions(ConfigManager.SettingsPermissions.User);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                if (!DataProvider.UserDao.IsExists(id))
                {
                    return(NotFound());
                }

                var user = UserManager.GetUserInfoByUserId(id);

                return(Ok(new
                {
                    Value = user
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 4
0
        public IHttpActionResult List()
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeUsers) ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasSystemPermissions(ConfigManager.SettingsPermissions.User);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var top  = request.GetQueryInt("top", 20);
                var skip = request.GetQueryInt("skip");

                var users = DataProvider.UserDao.GetUsers(skip, top);
                var count = DataProvider.UserDao.GetCount();

                return(Ok(new PageResponse(users, top, skip, request.HttpRequest.Url.AbsoluteUri)
                {
                    Count = count
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 5
0
        public IHttpActionResult Create([FromBody] AdministratorInfoCreateUpdate adminInfo)
        {
            try
            {
                var request         = new AuthenticatedRequest();
                var isApiAuthorized = request.IsApiAuthenticated && AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeAdministrators);
                if (!isApiAuthorized)
                {
                    return(Unauthorized());
                }

                var retval = DataProvider.AdministratorDao.ApiInsert(adminInfo, out var errorMessage);
                if (retval == null)
                {
                    return(BadRequest(errorMessage));
                }

                return(Ok(new
                {
                    Value = retval
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 6
0
        public IHttpActionResult CreateLog(int id, [FromBody] UserLogInfo logInfo)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeUsers) ||
                              request.IsUserLoggin &&
                              request.UserId == id ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasSystemPermissions(ConfigManager.SettingsPermissions.User);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var userInfo = UserManager.GetUserInfoByUserId(id);
                if (userInfo == null)
                {
                    return(NotFound());
                }

                var retval = DataProvider.UserLogDao.ApiInsert(userInfo.UserName, logInfo);

                return(Ok(new
                {
                    Value = retval
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 7
0
        public IHttpActionResult UploadAvatar(int id)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeUsers) ||
                              request.IsUserLoggin &&
                              request.UserId == id ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasSystemPermissions(ConfigManager.SettingsPermissions.User);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var userInfo = UserManager.GetUserInfoByUserId(id);
                if (userInfo == null)
                {
                    return(NotFound());
                }

                foreach (string name in HttpContext.Current.Request.Files)
                {
                    var postFile = HttpContext.Current.Request.Files[name];

                    if (postFile == null)
                    {
                        return(BadRequest("Could not read image from body"));
                    }

                    var fileName = UserManager.GetUserUploadFileName(postFile.FileName);
                    var filePath = UserManager.GetUserUploadPath(userInfo.Id, fileName);

                    if (!EFileSystemTypeUtils.IsImage(PathUtils.GetExtension(fileName)))
                    {
                        return(BadRequest("image file extension is not correct"));
                    }

                    DirectoryUtils.CreateDirectoryIfNotExists(filePath);
                    postFile.SaveAs(filePath);

                    userInfo.AvatarUrl = UserManager.GetUserUploadUrl(userInfo.Id, fileName);

                    DataProvider.UserDao.Update(userInfo);
                }

                return(Ok(new
                {
                    Value = userInfo
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 8
0
        public IHttpActionResult Update(int id)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeUsers) ||
                              request.IsUserLoggin &&
                              request.UserId == id ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasSystemPermissions(ConfigManager.SettingsPermissions.User);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var body = request.GetPostObject <Dictionary <string, object> >();

                if (body == null)
                {
                    return(BadRequest("Could not read user from body"));
                }

                var userInfo = UserManager.GetUserInfoByUserId(id);
                if (userInfo == null)
                {
                    return(NotFound());
                }

                var retval = DataProvider.UserDao.Update(userInfo, body, out var errorMessage);
                if (retval == null)
                {
                    return(BadRequest(errorMessage));
                }

                return(Ok(new
                {
                    Value = retval
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 9
0
        public IHttpActionResult ResetPassword(int id)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeUsers) ||
                              request.IsUserLoggin &&
                              request.UserId == id ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasSystemPermissions(ConfigManager.SettingsPermissions.User);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var userInfo = UserManager.GetUserInfoByUserId(id);
                if (userInfo == null)
                {
                    return(NotFound());
                }

                var password    = request.GetPostString("password");
                var newPassword = request.GetPostString("newPassword");

                if (!DataProvider.UserDao.CheckPassword(password, false, userInfo.Password, EPasswordFormatUtils.GetEnumType(userInfo.PasswordFormat), userInfo.PasswordSalt))
                {
                    return(BadRequest("原密码不正确,请重新输入"));
                }

                if (!DataProvider.UserDao.ChangePassword(userInfo.UserName, newPassword, out string errorMessage))
                {
                    return(BadRequest(errorMessage));
                }

                return(Ok(new
                {
                    Value = userInfo
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 10
0
        public IHttpActionResult Delete(int siteId, int channelId)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeChannels) ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasChannelPermissions(siteId, channelId,
                                                                             ConfigManager.ChannelPermissions.ChannelDelete);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                if (channelInfo == null)
                {
                    return(BadRequest("无法确定内容对应的栏目"));
                }

                var tableName = ChannelManager.GetTableName(siteInfo, channelId);
                DataProvider.ContentDao.UpdateTrashContentsByChannelId(siteId, channelId, tableName);
                DataProvider.ChannelDao.Delete(siteId, channelId);

                return(Ok(new
                {
                    Value = channelInfo.ToDictionary()
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 11
0
        public IHttpActionResult List(int siteId)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeChannels) ||
                              request.IsAdminLoggin;
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var root          = ChannelManager.GetChannelInfo(siteId, siteId);
                var channelIdList = ChannelManager.GetChannelIdList(root, EScopeType.Children);

                var dictInfoList = new List <Dictionary <string, object> >();
                foreach (var channelId in channelIdList)
                {
                    var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                    dictInfoList.Add(channelInfo.ToDictionary());
                }

                return(Ok(new
                {
                    Value = dictInfoList
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 12
0
        public IHttpActionResult GetLogs(int id)
        {
            try
            {
                var request = new RequestImpl();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeUsers) ||
                              request.IsUserLoggin &&
                              request.UserId == id ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasSystemPermissions(ConfigManager.SettingsPermissions.User);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var userInfo = UserManager.GetUserInfoByUserId(id);
                if (userInfo == null)
                {
                    return(NotFound());
                }

                var top  = request.GetQueryInt("top", 20);
                var skip = request.GetQueryInt("skip");

                var logs = DataProvider.UserLogDao.ApiGetLogs(userInfo.UserName, skip, top);

                return(Ok(new PageResponse(logs, top, skip, request.HttpRequest.Url.AbsoluteUri)
                {
                    Count = DataProvider.UserDao.GetCount()
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 13
0
        public IHttpActionResult Get(int siteId, int channelId)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeChannels) ||
                              request.IsAdminLoggin;
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                if (channelInfo == null)
                {
                    return(BadRequest("无法确定内容对应的栏目"));
                }

                channelInfo.Children = ChannelManager.GetChildren(siteId, channelId);

                return(Ok(new
                {
                    Value = channelInfo.ToDictionary()
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 14
0
        public IHttpActionResult Update(int id, [FromBody] AdministratorInfoCreateUpdate adminInfo)
        {
            try
            {
                var request         = new RequestImpl();
                var isApiAuthorized = request.IsApiAuthenticated && AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeAdministrators);
                if (!isApiAuthorized)
                {
                    return(Unauthorized());
                }

                if (adminInfo == null)
                {
                    return(BadRequest("Could not read administrator from body"));
                }

                if (!DataProvider.AdministratorDao.ApiIsExists(id))
                {
                    return(NotFound());
                }

                var retval = DataProvider.AdministratorDao.ApiUpdate(id, adminInfo, out var errorMessage);
                if (retval == null)
                {
                    return(BadRequest(errorMessage));
                }

                return(Ok(new
                {
                    Value = retval
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 15
0
        public IHttpActionResult ResetPassword()
        {
            try
            {
                var request         = new AuthenticatedRequest();
                var isApiAuthorized = request.IsApiAuthenticated && AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeAdministrators);
                if (!isApiAuthorized)
                {
                    return(Unauthorized());
                }

                var account     = request.GetPostString("account");
                var password    = request.GetPostString("password");
                var newPassword = request.GetPostString("newPassword");

                if (!DataProvider.AdministratorDao.Validate(account, password, true, out var userName, out var errorMessage))
                {
                    return(BadRequest(errorMessage));
                }

                var adminInfo = AdminManager.GetAdminInfoByUserName(userName);

                if (!DataProvider.AdministratorDao.ChangePassword(adminInfo, newPassword, out errorMessage))
                {
                    return(BadRequest(errorMessage));
                }

                return(Ok(new
                {
                    Value = adminInfo
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 16
0
        public IHttpActionResult GetSiteContents(int siteId)
        {
            try
            {
                var  request  = new AuthenticatedRequest();
                var  sourceId = request.GetPostInt(ContentAttribute.SourceId.ToCamelCase());
                bool isAuth;
                if (sourceId == SourceManager.User)
                {
                    isAuth = request.IsUserLoggin && request.UserPermissions.HasChannelPermissions(siteId, siteId, ConfigManager.ChannelPermissions.ContentView);
                }
                else
                {
                    isAuth = request.IsApiAuthenticated &&
                             AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeContents) ||
                             request.IsUserLoggin &&
                             request.UserPermissions.HasChannelPermissions(siteId, siteId,
                                                                           ConfigManager.ChannelPermissions.ContentView) ||
                             request.IsAdminLoggin &&
                             request.AdminPermissions.HasChannelPermissions(siteId, siteId,
                                                                            ConfigManager.ChannelPermissions.ContentView);
                }
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                if (!request.AdminPermissionsImpl.HasChannelPermissions(siteId, siteId,
                                                                        ConfigManager.ChannelPermissions.ContentView))
                {
                    return(Unauthorized());
                }

                var tableName = siteInfo.TableName;

                var parameters = new ApiContentsParameters(request);

                var tupleList = DataProvider.ContentDao.ApiGetContentIdListBySiteId(tableName, siteId, parameters, out var count);
                var value     = new List <Dictionary <string, object> >();
                foreach (var tuple in tupleList)
                {
                    var contentInfo = ContentManager.GetContentInfo(siteInfo, tuple.Item1, tuple.Item2);
                    if (contentInfo != null)
                    {
                        value.Add(contentInfo.ToDictionary());
                    }
                }

                return(Ok(new PageResponse(value, parameters.Top, parameters.Skip, request.HttpRequest.Url.AbsoluteUri)
                {
                    Count = count
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 17
0
        public QueryResult GetContents([FromBody] QueryRequest request)
        {
            var req       = new AuthenticatedRequest();
            var sourceId  = req.GetPostInt(ContentAttribute.SourceId.ToCamelCase());
            var channelId = request.ChannelId ?? request.SiteId;

            bool isAuth;

            if (sourceId == SourceManager.User)
            {
                isAuth = req.IsUserLoggin && req.UserPermissions.HasChannelPermissions(request.SiteId, channelId, ConfigManager.ChannelPermissions.ContentView);
            }
            else
            {
                isAuth = req.IsApiAuthenticated &&
                         AccessTokenManager.IsScope(req.ApiToken, AccessTokenManager.ScopeContents) ||
                         req.IsUserLoggin &&
                         req.UserPermissions.HasChannelPermissions(request.SiteId, channelId,
                                                                   ConfigManager.ChannelPermissions.ContentView) ||
                         req.IsAdminLoggin &&
                         req.AdminPermissions.HasChannelPermissions(request.SiteId, channelId,
                                                                    ConfigManager.ChannelPermissions.ContentView);
            }
            if (!isAuth)
            {
                return(Request.Unauthorized <QueryResult>());
            }

            var site = SiteManager.GetSiteInfo(request.SiteId);

            if (site == null)
            {
                return(Request.BadRequest <QueryResult>("无法确定内容对应的站点"));
            }

            var channelInfo = ChannelManager.GetChannelInfo(request.SiteId, channelId);

            if (channelInfo == null)
            {
                return(Request.BadRequest <QueryResult>("无法确定内容对应的栏目"));
            }

            if (!req.AdminPermissionsImpl.HasChannelPermissions(request.SiteId, channelId,
                                                                ConfigManager.ChannelPermissions.ContentView))
            {
                return(Request.Unauthorized <QueryResult>());
            }

            var tableName         = site.TableName;
            var query             = GetQuery(request.SiteId, request.ChannelId, request);
            var totalCount        = DataProvider.ContentDao.GetTotalCount(tableName, query);
            var channelContentIds = DataProvider.ContentDao.GetChannelContentIdList(tableName, query);

            var contents = new List <Dictionary <string, object> >();

            foreach (var channelContentId in channelContentIds)
            {
                var content = ContentManager.GetContentInfo(site, channelContentId.ChannelId, channelContentId.Id);
                contents.Add(content.ToDictionary());
            }

            return(new QueryResult
            {
                Contents = contents,
                TotalCount = totalCount
            });
        }
Esempio n. 18
0
        public StlRequest()
        {
            Request         = new RequestImpl();
            IsApiAuthorized = Request.IsApiAuthenticated && AccessTokenManager.IsScope(Request.ApiToken, AccessTokenManager.ScopeStl);

            if (!IsApiAuthorized)
            {
                return;
            }

            var siteId  = Request.GetQueryInt("siteId");
            var siteDir = Request.GetQueryString("siteDir");

            var channelId = Request.GetQueryInt("channelId");
            var contentId = Request.GetQueryInt("contentId");

            if (siteId > 0)
            {
                SiteInfo = SiteManager.GetSiteInfo(siteId);
            }
            else if (!string.IsNullOrEmpty(siteDir))
            {
                SiteInfo = SiteManager.GetSiteInfoByDirectory(siteDir);
            }
            else
            {
                SiteInfo = SiteManager.GetSiteInfoByIsRoot();
                if (SiteInfo == null)
                {
                    var siteInfoList = SiteManager.GetSiteInfoList();
                    if (siteInfoList != null && siteInfoList.Count > 0)
                    {
                        SiteInfo = siteInfoList[0];
                    }
                }
            }

            if (SiteInfo == null)
            {
                return;
            }

            if (channelId == 0)
            {
                channelId = SiteInfo.Id;
            }

            var templateInfo = new TemplateInfo(0, SiteInfo.Id, string.Empty, TemplateType.IndexPageTemplate, string.Empty, string.Empty, string.Empty, ECharset.utf_8, true);

            PageInfo = new PageInfo(channelId, contentId, SiteInfo, templateInfo, new Dictionary <string, object>())
            {
                UniqueId = 1000,
                UserInfo = Request.UserInfo
            };

            var attributes = TranslateUtils.NewIgnoreCaseNameValueCollection();

            foreach (var key in Request.QueryString.AllKeys)
            {
                attributes[key] = Request.QueryString[key];
            }

            ContextInfo = new ContextInfo(PageInfo)
            {
                IsStlEntity = true,
                Attributes  = attributes,
                InnerHtml   = string.Empty
            };
        }
Esempio n. 19
0
        public IHttpActionResult GetChannelContents(int siteId, int channelId)
        {
            try
            {
                var  request  = new RequestImpl();
                var  sourceId = request.GetPostInt(ContentAttribute.SourceId.ToCamelCase());
                bool isAuth;
                if (sourceId == SourceManager.User)
                {
                    isAuth = request.IsUserLoggin && request.UserPermissions.HasChannelPermissions(siteId, channelId, ConfigManager.ChannelPermissions.ContentView);
                }
                else
                {
                    isAuth = request.IsApiAuthenticated &&
                             AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeContents) ||
                             request.IsUserLoggin &&
                             request.UserPermissions.HasChannelPermissions(siteId, channelId,
                                                                           ConfigManager.ChannelPermissions.ContentView) ||
                             request.IsAdminLoggin &&
                             request.AdminPermissions.HasChannelPermissions(siteId, channelId,
                                                                            ConfigManager.ChannelPermissions.ContentView);
                }
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                if (channelInfo == null)
                {
                    return(BadRequest("无法确定内容对应的栏目"));
                }

                if (!request.AdminPermissionsImpl.HasChannelPermissions(siteId, channelId,
                                                                        ConfigManager.ChannelPermissions.ContentView))
                {
                    return(Unauthorized());
                }

                var tableName = ChannelManager.GetTableName(siteInfo, channelInfo);

                var top     = request.GetQueryInt("top", 20);
                var skip    = request.GetQueryInt("skip");
                var like    = request.GetQueryString("like");
                var orderBy = request.GetQueryString("orderBy");

                int count;
                var contentIdList = DataProvider.ContentDao.ApiGetContentIdListByChannelId(tableName, siteId, channelId, top, skip, like, orderBy, request.QueryString, out count);
                var value         = new List <Dictionary <string, object> >();
                foreach (var contentId in contentIdList)
                {
                    var contentInfo = ContentManager.GetContentInfo(siteInfo, channelInfo, contentId);
                    if (contentInfo != null)
                    {
                        value.Add(contentInfo.ToDictionary());
                    }
                }

                return(Ok(new PageResponse(value, top, skip, request.HttpRequest.Url.AbsoluteUri)
                {
                    Count = count
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 20
0
        public IHttpActionResult Get(int siteId, int channelId, int id)
        {
            try
            {
                var  request  = new RequestImpl();
                var  sourceId = request.GetPostInt(ContentAttribute.SourceId.ToCamelCase());
                bool isAuth;
                if (sourceId == SourceManager.User)
                {
                    isAuth = request.IsUserLoggin && request.UserPermissions.HasChannelPermissions(siteId, channelId, ConfigManager.ChannelPermissions.ContentView);
                }
                else
                {
                    isAuth = request.IsApiAuthenticated &&
                             AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeContents) ||
                             request.IsUserLoggin &&
                             request.UserPermissions.HasChannelPermissions(siteId, channelId,
                                                                           ConfigManager.ChannelPermissions.ContentView) ||
                             request.IsAdminLoggin &&
                             request.AdminPermissions.HasChannelPermissions(siteId, channelId,
                                                                            ConfigManager.ChannelPermissions.ContentView);
                }
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                if (channelInfo == null)
                {
                    return(BadRequest("无法确定内容对应的栏目"));
                }

                if (!request.AdminPermissionsImpl.HasChannelPermissions(siteId, channelId,
                                                                        ConfigManager.ChannelPermissions.ContentView))
                {
                    return(Unauthorized());
                }

                var contentInfo = ContentManager.GetContentInfo(siteInfo, channelInfo, id);
                if (contentInfo == null)
                {
                    return(NotFound());
                }

                return(Ok(new
                {
                    Value = contentInfo
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 21
0
        public IHttpActionResult Create(int siteId, int channelId)
        {
            try
            {
                var  request  = new RequestImpl();
                var  sourceId = request.GetPostInt(ContentAttribute.SourceId.ToCamelCase());
                bool isAuth;
                if (sourceId == SourceManager.User)
                {
                    isAuth = request.IsUserLoggin && request.UserPermissions.HasChannelPermissions(siteId, channelId, ConfigManager.ChannelPermissions.ContentAdd);
                }
                else
                {
                    isAuth = request.IsApiAuthenticated &&
                             AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeContents) ||
                             request.IsUserLoggin &&
                             request.UserPermissions.HasChannelPermissions(siteId, channelId,
                                                                           ConfigManager.ChannelPermissions.ContentAdd) ||
                             request.IsAdminLoggin &&
                             request.AdminPermissions.HasChannelPermissions(siteId, channelId,
                                                                            ConfigManager.ChannelPermissions.ContentAdd);
                }
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                if (channelInfo == null)
                {
                    return(BadRequest("无法确定内容对应的栏目"));
                }

                if (!channelInfo.Additional.IsContentAddable)
                {
                    return(BadRequest("此栏目不能添加内容"));
                }

                var attributes = request.GetPostObject <Dictionary <string, object> >();
                if (attributes == null)
                {
                    return(BadRequest("无法从body中获取内容实体"));
                }
                var checkedLevel = request.GetPostInt("checkedLevel");

                var tableName = ChannelManager.GetTableName(siteInfo, channelInfo);
                var adminName = request.AdminName;

                var isChecked = checkedLevel >= siteInfo.Additional.CheckContentLevel;
                if (isChecked)
                {
                    if (sourceId == SourceManager.User || request.IsUserLoggin)
                    {
                        isChecked = request.UserPermissionsImpl.HasChannelPermissions(siteId, channelId,
                                                                                      ConfigManager.ChannelPermissions.ContentCheck);
                    }
                    else if (request.IsAdminLoggin)
                    {
                        isChecked = request.AdminPermissionsImpl.HasChannelPermissions(siteId, channelId,
                                                                                       ConfigManager.ChannelPermissions.ContentCheck);
                    }
                }

                var contentInfo = new ContentInfo(attributes)
                {
                    SiteId           = siteId,
                    ChannelId        = channelId,
                    AddUserName      = adminName,
                    LastEditDate     = DateTime.Now,
                    LastEditUserName = adminName,
                    AdminId          = request.AdminId,
                    UserId           = request.UserId,
                    SourceId         = sourceId,
                    IsChecked        = isChecked,
                    CheckedLevel     = checkedLevel
                };

                contentInfo.Id = DataProvider.ContentDao.Insert(tableName, siteInfo, channelInfo, contentInfo);

                foreach (var service in PluginManager.Services)
                {
                    try
                    {
                        service.OnContentFormSubmit(new ContentFormSubmitEventArgs(siteId, channelId, contentInfo.Id, new AttributesImpl(attributes), contentInfo));
                    }
                    catch (Exception ex)
                    {
                        LogUtils.AddErrorLog(service.PluginId, ex, nameof(IService.ContentFormSubmit));
                    }
                }

                if (contentInfo.IsChecked)
                {
                    CreateManager.CreateContent(siteId, channelId, contentInfo.Id);
                    CreateManager.TriggerContentChangedEvent(siteId, channelId);
                }

                request.AddSiteLog(siteId, channelId, contentInfo.Id, "添加内容",
                                   $"栏目:{ChannelManager.GetChannelNameNavigation(siteId, contentInfo.ChannelId)},内容标题:{contentInfo.Title}");

                return(Ok(new
                {
                    Value = contentInfo
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 22
0
        public IHttpActionResult Create(int siteId)
        {
            try
            {
                var request  = new AuthenticatedRequest();
                var parentId = request.GetPostInt(ChannelAttribute.ParentId, siteId);

                var isAuth = request.IsApiAuthenticated &&
                             AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeChannels) ||
                             request.IsAdminLoggin &&
                             request.AdminPermissions.HasChannelPermissions(siteId, parentId,
                                                                            ConfigManager.ChannelPermissions.ChannelAdd);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var contentModelPluginId    = request.GetPostString(ChannelAttribute.ContentModelPluginId);
                var contentRelatedPluginIds = request.GetPostString(ChannelAttribute.ContentRelatedPluginIds);

                var channelName         = request.GetPostString(ChannelAttribute.ChannelName);
                var indexName           = request.GetPostString(ChannelAttribute.IndexName);
                var filePath            = request.GetPostString(ChannelAttribute.FilePath);
                var channelFilePathRule = request.GetPostString(ChannelAttribute.ChannelFilePathRule);
                var contentFilePathRule = request.GetPostString(ChannelAttribute.ContentFilePathRule);
                var groupNameCollection = request.GetPostString(ChannelAttribute.GroupNameCollection);
                var imageUrl            = request.GetPostString(ChannelAttribute.ImageUrl);
                var content             = request.GetPostString(ChannelAttribute.Content);
                var keywords            = request.GetPostString(ChannelAttribute.Keywords);
                var description         = request.GetPostString(ChannelAttribute.Description);
                var linkUrl             = request.GetPostString(ChannelAttribute.LinkUrl);
                var linkType            = request.GetPostString(ChannelAttribute.LinkType);
                var channelTemplateId   = request.GetPostInt(ChannelAttribute.ChannelTemplateId);
                var contentTemplateId   = request.GetPostInt(ChannelAttribute.ContentTemplateId);

                var channelInfo = new ChannelInfo
                {
                    SiteId                  = siteId,
                    ParentId                = parentId,
                    ContentModelPluginId    = contentModelPluginId,
                    ContentRelatedPluginIds = contentRelatedPluginIds
                };

                if (!string.IsNullOrEmpty(indexName))
                {
                    var indexNameList = DataProvider.ChannelDao.GetIndexNameList(siteId);
                    if (indexNameList.IndexOf(indexName) != -1)
                    {
                        return(BadRequest("栏目添加失败,栏目索引已存在!"));
                    }
                }

                if (!string.IsNullOrEmpty(filePath))
                {
                    if (!DirectoryUtils.IsDirectoryNameCompliant(filePath))
                    {
                        return(BadRequest("栏目页面路径不符合系统要求!"));
                    }

                    if (PathUtils.IsDirectoryPath(filePath))
                    {
                        filePath = PageUtils.Combine(filePath, "index.html");
                    }

                    var filePathList = DataProvider.ChannelDao.GetAllFilePathBySiteId(siteId);
                    if (filePathList.IndexOf(filePath) != -1)
                    {
                        return(BadRequest("栏目添加失败,栏目页面路径已存在!"));
                    }
                }

                if (!string.IsNullOrEmpty(channelFilePathRule))
                {
                    if (!DirectoryUtils.IsDirectoryNameCompliant(channelFilePathRule))
                    {
                        return(BadRequest("栏目页面命名规则不符合系统要求!"));
                    }
                    if (PathUtils.IsDirectoryPath(channelFilePathRule))
                    {
                        return(BadRequest("栏目页面命名规则必须包含生成文件的后缀!"));
                    }
                }

                if (!string.IsNullOrEmpty(contentFilePathRule))
                {
                    if (!DirectoryUtils.IsDirectoryNameCompliant(contentFilePathRule))
                    {
                        return(BadRequest("内容页面命名规则不符合系统要求!"));
                    }
                    if (PathUtils.IsDirectoryPath(contentFilePathRule))
                    {
                        return(BadRequest("内容页面命名规则必须包含生成文件的后缀!"));
                    }
                }

                //var parentChannelInfo = ChannelManager.GetChannelInfo(siteId, parentId);
                //var styleInfoList = TableStyleManager.GetChannelStyleInfoList(parentChannelInfo);
                //var extendedAttributes = BackgroundInputTypeParser.SaveAttributes(siteInfo, styleInfoList, Request.Form, null);
                channelInfo.Additional.Load(request.GetPostObject <Dictionary <string, object> >());
                //foreach (string key in attributes)
                //{
                //    channelInfo.Additional.SetExtendedAttribute(key, attributes[key]);
                //}

                channelInfo.ChannelName         = channelName;
                channelInfo.IndexName           = indexName;
                channelInfo.FilePath            = filePath;
                channelInfo.ChannelFilePathRule = channelFilePathRule;
                channelInfo.ContentFilePathRule = contentFilePathRule;

                channelInfo.GroupNameCollection = groupNameCollection;
                channelInfo.ImageUrl            = imageUrl;
                channelInfo.Content             = content;
                channelInfo.Keywords            = keywords;
                channelInfo.Description         = description;
                channelInfo.LinkUrl             = linkUrl;
                channelInfo.LinkType            = linkType;
                channelInfo.ChannelTemplateId   = channelTemplateId;
                channelInfo.ContentTemplateId   = contentTemplateId;

                channelInfo.AddDate = DateTime.Now;
                channelInfo.Id      = DataProvider.ChannelDao.Insert(channelInfo);
                //栏目选择投票样式后,内容

                CreateManager.CreateChannel(siteId, channelInfo.Id);

                request.AddSiteLog(siteId, "添加栏目", $"栏目:{channelName}");

                return(Ok(new
                {
                    Value = channelInfo.ToDictionary()
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 23
0
        public CheckResult Check([FromBody] CheckRequest request)
        {
            var req = new AuthenticatedRequest();

            if (!req.IsApiAuthenticated ||
                !AccessTokenManager.IsScope(req.ApiToken, AccessTokenManager.ScopeContents))
            {
                return(Request.Unauthorized <CheckResult>());
            }

            var site = SiteManager.GetSiteInfo(request.SiteId);

            if (site == null)
            {
                return(Request.BadRequest <CheckResult>("无法确定内容对应的站点"));
            }

            var contents = new List <Dictionary <string, object> >();

            foreach (var channelContentId in request.Contents)
            {
                var channel   = ChannelManager.GetChannelInfo(request.SiteId, channelContentId.ChannelId);
                var tableName = ChannelManager.GetTableName(site, channel);
                var content   = ContentManager.GetContentInfo(site, channel, channelContentId.Id);
                if (content == null)
                {
                    continue;
                }

                content.Set(ContentAttribute.CheckUserName, req.AdminName);
                content.Set(ContentAttribute.CheckDate, DateTime.Now);
                content.Set(ContentAttribute.CheckReasons, request.Reasons);
                content.Checked      = true;
                content.CheckedLevel = 0;

                DataProvider.ContentDao.Update(site, channel, content);

                contents.Add(content.ToDictionary());

                var contentCheck = new ContentCheckInfo
                {
                    TableName    = tableName,
                    SiteId       = request.SiteId,
                    ChannelId    = content.ChannelId,
                    ContentId    = content.Id,
                    UserName     = req.AdminName,
                    IsChecked    = true,
                    CheckedLevel = 0,
                    CheckDate    = DateTime.Now,
                    Reasons      = request.Reasons
                };

                DataProvider.ContentCheckDao.Insert(contentCheck);
            }

            req.AddSiteLog(request.SiteId, "批量审核内容");

            foreach (var content in request.Contents)
            {
                CreateManager.CreateContent(request.SiteId, content.ChannelId, content.Id);
            }

            foreach (var distinctChannelId in request.Contents.Select(x => x.ChannelId).Distinct())
            {
                CreateManager.TriggerContentChangedEvent(request.SiteId, distinctChannelId);
            }

            CreateManager.CreateChannel(request.SiteId, request.SiteId);

            return(new CheckResult
            {
                Contents = contents
            });
        }
Esempio n. 24
0
        public IHttpActionResult Update(int siteId, int channelId)
        {
            try
            {
                var request = new AuthenticatedRequest();
                var isAuth  = request.IsApiAuthenticated &&
                              AccessTokenManager.IsScope(request.ApiToken, AccessTokenManager.ScopeChannels) ||
                              request.IsAdminLoggin &&
                              request.AdminPermissions.HasChannelPermissions(siteId, channelId,
                                                                             ConfigManager.ChannelPermissions.ChannelEdit);
                if (!isAuth)
                {
                    return(Unauthorized());
                }

                var siteInfo = SiteManager.GetSiteInfo(siteId);
                if (siteInfo == null)
                {
                    return(BadRequest("无法确定内容对应的站点"));
                }

                var channelInfo = ChannelManager.GetChannelInfo(siteId, channelId);
                if (channelInfo == null)
                {
                    return(BadRequest("无法确定内容对应的栏目"));
                }

                channelInfo.Additional.Load(request.GetPostObject <Dictionary <string, object> >());

                if (request.IsPostExists(ChannelAttribute.ChannelName))
                {
                    channelInfo.ChannelName = request.GetPostString(ChannelAttribute.ChannelName);
                }

                if (request.IsPostExists(ChannelAttribute.IndexName))
                {
                    var indexName = request.GetPostString(ChannelAttribute.IndexName);
                    if (!channelInfo.IndexName.Equals(indexName) && !string.IsNullOrEmpty(indexName))
                    {
                        var indexNameList = DataProvider.ChannelDao.GetIndexNameList(siteId);
                        if (indexNameList.IndexOf(indexName) != -1)
                        {
                            return(BadRequest("栏目属性修改失败,栏目索引已存在!"));
                        }
                    }
                    channelInfo.IndexName = indexName;
                }

                if (request.IsPostExists(ChannelAttribute.ContentModelPluginId))
                {
                    var contentModelPluginId = request.GetPostString(ChannelAttribute.ContentModelPluginId);
                    if (channelInfo.ContentModelPluginId != contentModelPluginId)
                    {
                        channelInfo.ContentModelPluginId = contentModelPluginId;
                    }
                }

                if (request.IsPostExists(ChannelAttribute.ContentRelatedPluginIds))
                {
                    channelInfo.ContentRelatedPluginIds = request.GetPostString(ChannelAttribute.ContentRelatedPluginIds);
                }

                if (request.IsPostExists(ChannelAttribute.FilePath))
                {
                    var filePath = request.GetPostString(ChannelAttribute.FilePath);
                    filePath = filePath.Trim();
                    if (!channelInfo.FilePath.Equals(filePath) && !string.IsNullOrEmpty(filePath))
                    {
                        if (!DirectoryUtils.IsDirectoryNameCompliant(filePath))
                        {
                            return(BadRequest("栏目页面路径不符合系统要求!"));
                        }

                        if (PathUtils.IsDirectoryPath(filePath))
                        {
                            filePath = PageUtils.Combine(filePath, "index.html");
                        }

                        var filePathList = DataProvider.ChannelDao.GetAllFilePathBySiteId(siteId);
                        if (filePathList.IndexOf(filePath) != -1)
                        {
                            return(BadRequest("栏目修改失败,栏目页面路径已存在!"));
                        }
                    }
                    channelInfo.FilePath = filePath;
                }

                if (request.IsPostExists(ChannelAttribute.ChannelFilePathRule))
                {
                    var channelFilePathRule = request.GetPostString(ChannelAttribute.ChannelFilePathRule);

                    if (!string.IsNullOrEmpty(channelFilePathRule))
                    {
                        var filePathRule = channelFilePathRule.Replace("|", string.Empty);
                        if (!DirectoryUtils.IsDirectoryNameCompliant(filePathRule))
                        {
                            return(BadRequest("栏目页面命名规则不符合系统要求!"));
                        }
                        if (PathUtils.IsDirectoryPath(filePathRule))
                        {
                            return(BadRequest("栏目页面命名规则必须包含生成文件的后缀!"));
                        }
                    }

                    channelInfo.ChannelFilePathRule = channelFilePathRule;
                }

                if (request.IsPostExists(ChannelAttribute.ContentFilePathRule))
                {
                    var contentFilePathRule = request.GetPostString(ChannelAttribute.ContentFilePathRule);

                    if (!string.IsNullOrEmpty(contentFilePathRule))
                    {
                        var filePathRule = contentFilePathRule.Replace("|", string.Empty);
                        if (!DirectoryUtils.IsDirectoryNameCompliant(filePathRule))
                        {
                            return(BadRequest("内容页面命名规则不符合系统要求!"));
                        }
                        if (PathUtils.IsDirectoryPath(filePathRule))
                        {
                            return(BadRequest("内容页面命名规则必须包含生成文件的后缀!"));
                        }
                    }

                    channelInfo.ContentFilePathRule = contentFilePathRule;
                }

                if (request.IsPostExists(ChannelAttribute.GroupNameCollection))
                {
                    channelInfo.GroupNameCollection = request.GetPostString(ChannelAttribute.GroupNameCollection);
                }

                if (request.IsPostExists(ChannelAttribute.ImageUrl))
                {
                    channelInfo.ImageUrl = request.GetPostString(ChannelAttribute.ImageUrl);
                }

                if (request.IsPostExists(ChannelAttribute.Content))
                {
                    channelInfo.Content = request.GetPostString(ChannelAttribute.Content);
                }

                if (request.IsPostExists(ChannelAttribute.Keywords))
                {
                    channelInfo.Keywords = request.GetPostString(ChannelAttribute.Keywords);
                }

                if (request.IsPostExists(ChannelAttribute.Description))
                {
                    channelInfo.Description = request.GetPostString(ChannelAttribute.Description);
                }

                if (request.IsPostExists(ChannelAttribute.LinkUrl))
                {
                    channelInfo.LinkUrl = request.GetPostString(ChannelAttribute.LinkUrl);
                }

                if (request.IsPostExists(ChannelAttribute.LinkType))
                {
                    channelInfo.LinkType = request.GetPostString(ChannelAttribute.LinkType);
                }

                if (request.IsPostExists(ChannelAttribute.ChannelTemplateId))
                {
                    channelInfo.ChannelTemplateId = request.GetPostInt(ChannelAttribute.ChannelTemplateId);
                }

                if (request.IsPostExists(ChannelAttribute.ContentTemplateId))
                {
                    channelInfo.ContentTemplateId = request.GetPostInt(ChannelAttribute.ContentTemplateId);
                }

                DataProvider.ChannelDao.Update(channelInfo);

                return(Ok(new
                {
                    Value = channelInfo.ToDictionary()
                }));
            }
            catch (Exception ex)
            {
                LogUtils.AddErrorLog(ex);
                return(InternalServerError(ex));
            }
        }
Esempio n. 25
0
 public bool IsAuthorized(string scope)
 {
     return(AccessTokenManager.IsScope(AuthRequest.ApiToken, scope));
 }