允许用户更改模板的最佳方式是什么?

发布于 2024-11-03 13:12:07 字数 1696 浏览 0 评论 0原文

在我的一个项目中,我的用户将拥有我网站的专用部分 ({username}.example.org)。在该子域中,我计划允许他们轻松更改设计。 我查看了一些已经这样做的在线网站,例如 TumblrPosterousShopify

关于这个应用程序的语言,我从 Django 开始,我成功地实现了一个“基于 url 名称的模板渲染,默认为后备”(http://someuser.example.com 将加载templates/someuser/* 或 templates/generic/* 中的模板(如果在第一个中找不到)。 但如果 PHP 或 Play!Framework 更适合我的需求,我可以将其导出。

使用的模板引擎是Jinja2,因为它和Django模板引擎一样简单,易于学习,并且安全(Python 代码不能(通常应该!)被执行)。

这是我发现的每个解决方案的优点/缺点。我真的很感激你对此的想法以及你会采取哪种方式,为什么。谢谢。

注意:用户将对 HTML/CSS 有很好的了解。

更改变量(如标题颜色等)

  • 优点:

    1. 简单&安全
  • 缺点:

    1. 功能太有限,想要个性化其网站的用户将无法使用此解决方案实现这一点

仅上传 CSS

  • 优点:

    1. 易于集成
    2. 安全
  • 缺点:

    1. 有限
    2. 用户图像存储在哪里? (徽标、背景、一些渐变等)

允许用户编辑模板(存储在数据库中)

  • 优点:

    1. 改变更重要
    2. 用户(几乎)可以自由地做任何他想做的事情(实现 GA、FeedBurner 等)
    3. 优点
  • 缺点:

    1. 他们将静态文件(徽标、背景图像、一些特殊效果(渐变))放在哪里?
    2. 模板存储在数据库中,每显示一页都需要一次 SQL 请求

允许用户编辑模板(存储在文件中)

  • 优点:

    1. 改变更重要
    2. 用户(几乎)可以自由地做任何他想做的事情(实现 GA、FeedBurner 等)
    3. 可以启用 FTP 访问,将用户置于其模板目录中。
  • 缺点:

    1. 静态文件也有同样的问题
    2. 缺点

此外,我遇到的问题是如何处理静态文件(图像、css、js):我看不到如何在 Apache(或 NGinx)中定义 VirtualHost ),这将请求数据库查看哪些用户属于该 url。

感谢您的帮助,我很感激!

In one of my project, my users will have a dedicated part of my website ({username}.example.org). In that subdomain, I plan to allow them to change the design easily.
I took a look at some online website that already does that, like Tumblr, Posterous and Shopify.

Regarding the language of this application, I started it with Django, which I succeedly implemented a "template rendering based on the url name with default as fallback" (http://someuser.example.com will load the templates in templates/someuser/* or in templates/generic/* if not found in the first).
But I can export it to PHP or Play!Framework if they are more adapted to my needs.

The template engine used is Jinja2, since it is as simple as Django template engine, easy to learn, and safe (no Python coode can (should normally!) be executed).

Here is the Pros/Cons of each solution I found. I would really appreciate your thoughts on that and which way you would do, why. Thanks.

Note: The users will have a good knowledge in HTML/CSS.

Changing vars (like title color, etc)

  • Pros:

    1. Simple & secure
  • Cons:

    1. Too limited, an user that want to personalize his website won't be able to do so with this solution

Only upload CSS

  • Pros:

    1. Simple to integrate
    2. Secure
  • Cons:

    1. Limited
    2. Where the user image are stored? (logo, background, some gradient, etc)

Allow user to edit templates (stored in the database)

  • Pros:

    1. Changes are more important
    2. The user is (almost) free to do whatever he wants (implements GA, FeedBurner, etc)
  • Cons:

    1. Where do they put the static files (logo, background-image, some special effect (gradient))?
    2. Template are stored in the database, which require one more SQL request for each page displayed

Allow user to edit templates (stored in files)

  • Pros:

    1. Changes are more important
    2. The user is (almost) free to do whatever he wants (implements GA, FeedBurner, etc)
    3. A FTP access can be enabled, rooting the user in his Template dir.
  • Cons:

    1. The same problem for the static files

Also, where I'm stuck is about how to handle static files (images, css, js) : I can't see how to define a VirtualHost in Apache (or NGinx) that would request the database to see which users belongs this url.

Thanks for your help, I appreciate!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆离笙 2024-11-10 13:12:07

好吧,我会根据我所做的和我的研究来回答自己。

正如史蒂夫提到的,您必须非常小心地让用户自定义页面布局。

风险必须得到充分评估!

就我而言,让用户更改完整的 HTML/CSS/JS 是可能的,就像 PosterousTumblr 可以。

风险是:

  • 用户可以添加 JavaScript 代码来收集其他用户的身份验证 cookie。这样做,该用户可以访问每个其他用户的管理部分的任何令牌身份验证。 一个简单的解决方案是避免管理部分和用户网站之间存在交叉 cookie。
  • 用户可以尝试在模板中执行代码,例如 Python、PHP、Java、Ruby 等(无论它是什么)用过的)。 这里的解决方案是使用一个模板引擎,该引擎完全不允许使用代码,而只能使用标签Jinja2 for python 非常适合这一点。

如果这两个条件得到很好的评估,选项“允许用户编辑模板(存储在数据库中)”是一个很好的解决方案。

但是,如果您担心数据库可能有太多点击,则最后一个解决方案,又名“允许用户编辑模板(存储在文件中)”,可能是可能的,如果:

  • 您确保用户无法访问他可以访问的模板目录的其他文件夹。 您可以通过设置使用数据库进行用户访问的 FTP 服务器(例如带有 MySQL 的 ProFTPd)以及在其模板目录中对用户进行 chroot 来实现此目的。设置配额也非常重要,以避免您的用户使用他的模板目录作为库存设备;)

好吧,我想我已经涵盖了整个问题。我可能遗漏了一些要点,如果是这样,请添加评论或新帖子,我会完成答案。

Ok, I'll answer myself based on what I did and my research.

As Steve mentionned, you have to be very careful about letting users customise page layout.

The risk must be well evaluated!

In my case, letting users change the complete HTML/CSS/JS is possible, in a way, like Posterous or Tumblr does.

The risks are :

  • A user could add a javascript code that would collect auth cookie of other users. Doing so, this user could have access to any token auth for the admin part of each other users. A simple solution is to avoid cross cookies between the admin part and the user website.
  • A user could try to execute code in the template, like Python, PHP, Java, Ruby, etc (regarding what it is used). The solution here is to use a template engine that disallow entirely the use of code, only tags. Jinja2 for python is a perfect fit for that.

If those two conditions are well evaluated, the option "Allow user to edit templates (stored in the database)" is a good solution.

But if you are worried about too many hits the database could have, the last solution, aka "Allow user to edit templates (stored in files)", could be possible, if :

  • You make sure the user can't access other folder that the template directory he can have access. You can do it so with something like setting up a FTP server that use the database for user access (like ProFTPd with MySQL), and chrooting the user in his template dir. A quota is also very important to set up, in order to avoid your user to use his template dir as stockage device ;)

Well, I think I covered the whole problem. It's possible I miss some points, if it's the case, please add a comment or a new post and I will complete the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文