首页
/ Clearance 项目技术文档

Clearance 项目技术文档

2024-12-10 01:22:13作者:戚魁泉Nursing

本文档将详细介绍如何安装、使用 Clearance 项目,并解释其 API 使用方法。

1. 安装指南

Clearance 是一个基于 Rails 的认证系统,支持电子邮件和密码登录。为了使用 Clearance,您需要确保您的项目满足以下依赖条件:

  • Rails 版本 >= 6.1
  • Ruby 版本 >= 3.0.0

首先,在您的 Gemfile 中添加以下代码:

gem "clearance"

然后运行以下命令安装依赖项:

bundle install

安装完成后,运行以下命令以生成必要的配置文件和迁移脚本:

rails generate clearance:install

此命令将执行以下操作:

  • 在您的 User 模型中插入 Clearance::User
  • ApplicationController 中插入 Clearance::Controller
  • 创建一个初始化文件,以便进一步配置
  • 创建一个迁移文件,用于创建用户表或向现有表添加必要的列

2. 项目使用说明

Clearance 提供了多种方法来控制用户访问和会话管理。

访问控制

使用 require_login 过滤器来控制对控制器动作的访问:

class ArticlesController < ApplicationController
  before_action :require_login

  def index
    current_user.articles
  end
end

Clearance 还提供了路由约束,可以在路由层控制访问:

Blog::Application.routes.draw do
  constraints Clearance::Constraints::SignedIn.new { |user| user.admin? } do
    root to: "admin/dashboards#show", as: :admin_root
  end

  constraints Clearance::Constraints::SignedIn.new do
    root to: "dashboards#show", as: :signed_in_root
  end

  constraints Clearance::Constraints::SignedOut.new do
    root to: "marketing#index"
  end
end

辅助方法

在控制器、视图和辅助方法中使用 current_usersigned_in?signed_out?。例如:

<% if signed_in? %>
  <%= current_user.email %>
  <%= button_to "注销", sign_out_path, method: :delete %>
<% else %>
  <%= link_to "登录", sign_in_path %>
<% end %>

密码重置

当用户重置密码时,Clearance 会发送一封电子邮件。您应该更改 mailer_sender 默认值,用于电子邮件的“发件人”头部:

Clearance.configure do |config|
  config.mailer_sender = "reply@example.com"
end

多域名支持

您可以通过设置 cookie_domain 作为可调用对象来支持多个域名或其它特殊域名配置。传递给方法的第一个参数是 ActionDispatch::Request 对象。

Clearance.configure do |config|
  config.cookie_domain = lambda { |request| request.host }
end

集成 Rack 应用

Clearance 将其会话添加到 Rack 环境哈希中,以便中间件和其他 Rack 应用可以与其交互:

class Bubblegum::Middleware
  def initialize(app)
    @app = app
  end

  def call(env)
    if env[:clearance].signed_in?
      env[:clearance].current_user.bubble_gum
    end

    @app.call(env)
  end
end

3. 项目 API 使用文档

Clearance 的配置和功能主要通过其模块和类提供。以下是一些关键配置选项和类的简要说明:

配置选项

config/initializers/clearance.rb 中覆盖以下默认设置:

Clearance.configure do |config|
  config.allow_sign_up = true
  config.allow_password_reset = true
  # ... 更多配置
end

控制器

Clearance 提供了默认的控制器行为,您可以根据需要覆盖它们:

class PasswordsController < Clearance::PasswordsController
  # ... 自定义行为
end

class SessionsController < Clearance::SessionsController
  # ... 自定义行为
end

class UsersController < Clearance::UsersController
  # ... 自定义行为
end

重定向

Clearance 提供了简单的重定向方法,可以根据需要覆盖它们:

# ... 自定义重定向方法

视图

覆盖 Clearance 的默认视图,以自定义表单、邮件等:

app/views/clearance_mailer/change_password.html.erb
app/views/passwords/create.html.erb
# ... 更多视图文件

布局

config/application.rb 中指定 Clearance 使用的布局:

config.to_prepare do
  Clearance::PasswordsController.layout "my_passwords_layout"
  # ... 更多布局配置
end

国际化

所有闪烁消息和电子邮件主题行都存储在国际化翻译中。像任何其他翻译一样覆盖它们:

# ... 自定义翻译

4. 项目安装方式

请参考“安装指南”部分,按照步骤进行安装。

  • 添加 Clearance 到 Gemfile
  • 运行 bundle install
  • 运行 rails generate clearance:install

完成这些步骤后,您就可以开始使用 Clearance 了。

热门项目推荐
相关项目推荐

项目优选

收起
MateChatMateChat
前端智能化场景解决方案UI库,轻松构建你的AI应用,我们将持续完善更新,欢迎你的使用与建议。 官网地址:https://matechat.gitcode.com
383
36
Python-100-DaysPython-100-Days
Python - 100天从新手到大师
Python
611
115
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
205
58
Ffit-framework
FIT: 企业级AI开发框架,提供多语言函数引擎(FIT)、流式编排引擎(WaterFlow)及Java生态的LangChain替代方案(FEL)。原生/Spring双模运行,支持插件热插拔与智能聚散部署,无缝统一大模型与业务系统。
Java
113
13
RuoYi-Cloud-Vue3RuoYi-Cloud-Vue3
🎉 基于Spring Boot、Spring Cloud & Alibaba、Vue3 & Vite、Element Plus的分布式前后端分离微服务架构权限管理系统
Vue
45
29
cjoycjoy
a fast,lightweight and joy web framework
Cangjie
11
2
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
286
79
hertzhertz
Go 微服务 HTTP 框架,具有高易用性、高性能、高扩展性等特点。
Go
7
1
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
60
48
open-eBackupopen-eBackup
open-eBackup是一款开源备份软件,采用集群高扩展架构,通过应用备份通用框架、并行备份等技术,为主流数据库、虚拟化、文件系统、大数据等应用提供E2E的数据备份、恢复等能力,帮助用户实现关键数据高效保护。
HTML
90
65