`
peryt
  • 浏览: 54405 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

write a authenticate method

 
阅读更多

chapter 7.2.4

 

1. we need to create a user, save it to database

2. we need to get the user object by email

3. we need to verify that it has a given password

 

not surprising, we will start from test again, right, TDD!!!!!

 

we expect we will have a class method:

 

 

User.authenticate(email, submitted_password)
 

it should return nil if email/password combination is invalid

it should return nil if no user exists with the given email

it should return the user object itself on success.

 

from this points, we can write the test:

 

 

describe "authenticate method" do
	it "should return nil on email/password mismatch" do
		wrong_password_user = User.authenticate(@attr[:email], "wrongpasss")
	end
	it "should return nil for an email address with no user" do
		nonexistent_user = User.authenticate("bar@foo.com", @attr[:password])
	end
	it "should return the user on email/password match" do
		matching_user = User.authenticate(@attr[:email], @attr[:password])
		matching_user.should == @user
	end
end

 

(for a class method, when using inside the class definition, we can omit the class name)

 

now it is the turn of authenticate method definition:

 

 

def self.authenticate(email, submitted_password)
	user = find_by_email(email)
	return nil if user.nil?
	return user if user.has_password?(submitted_password)
end

 

note, since find_by_email class method is called inside the class, the class name can be omitte.

 

the method will return the value of the last statement.

 

 

2. what is self?

 

a. inside the method definition, self means the object itself.

b. when define a class method, self means the Class itself.

 

def self.authenticate

end

 

c. another way to define a class method:

 

def User.authenticate

end

 

d. another way to defind a class method

 

class << self

def authenticate

end

end


the last way of definition is very weird, you don't need to use it by yourself, but you need to know it, in case you come across it when others' codes.

分享到:
评论

相关推荐

    Cvsnt 的问题authenticate

    The reason is you account in window xp did not have "administrator" privacy

    解决java.lang.SecurityException: JCE cannot authenticate the provider BC问题

    在与银联的对接中,调试过程中报错或使用类似登入加密:java.lang.SecurityException: JCE cannot authenticate the provider BC 进行问题解决,里面包含 bcprov-jdk16-143.jar与bcprov-jdk15-135.jar与具体文件存放...

    jboss7.1.1下报java.lang.SecurityException: JCE cannot authenticate the provider BC

    标题中的“jboss7.1.1下报java.lang.SecurityException: JCE cannot authenticate the provider BC”是一个典型的Java安全异常,通常发生在尝试使用不受信任的加密提供者时。在这个问题中,"BC"指的是Bouncy Castle...

    vue-authenticate, 简单 Vue.js 身份验证库.zip

    vue-authenticate, 简单 Vue.js 身份验证库 [ WARNING ]: 自述文件目前正在重写中,即将发布。身份验证 使用 Vue.js,提供本地登录/注册,以及使用 Github,Facebook,Google和其他OAuth提供商,可以方便地为提供...

    Dr.COM Authenticate Client Installer网通宽带插件

    Dr.COM Authenticate Client Installer网通宽带插件

    Mifare HID VB-Sample.zip_1K_DEMO_mifare_vb hid

    Mifare Demo HID USB modem, allow to read, write, authenticate and generate Access Bits, Visual Basic

    Authenticate:认证服务

    "Authenticate:认证服务"这个标题暗示了我们将会深入探讨如何利用编程语言如Perl来实现这样的服务。 Perl是一种功能强大的脚本语言,特别适合处理文本、网络和系统管理任务,包括构建安全的认证系统。在描述中只...

    authenticate.rar

    标题“authenticate.rar”暗示了这个压缩包可能包含与身份验证(Authenticate)相关的文件,这在IT行业中是一个关键的安全概念。身份验证是网络安全的基础,确保只有合法用户能够访问系统或资源。在这个上下文中,...

    servlet2.4doc

    The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. doGet...

    www-authenticate:缺少用于超级0.10.x的HTTP WWW-Authenticate标头解析器

    在标题中提到的问题,“www-authenticate:缺少用于超级0.10.x的HTTP WWW-Authenticate标头解析器”,意味着在特定版本的Hyper库中,对于"WWW-Authenticate"头的处理存在缺失或不兼容的情况。 Hyper是一个用Rust编程...

    Small-OS智能卡的INTERNAL AUTHENTICATE命令

    INTERNAL AUTHENTICATE命令用于经口令ˉ回令的过程来鉴别智能卡。一个8字节的随机数被送到智能卡, 它用DES算法给以加密。所用密钥的编号必须在参数”中给出,它必须指明所用之密钥是位于MF的BF的文件 中,还是在...

    ocb_encrypt_authenticate_memory.rar_memory

    "OCB_encrypt_authenticate_memory"这个主题涉及到一种特定的加密模式——OCB(Offset Codebook)模式,它是在块密码模式下的一个高效、安全的选择,尤其适用于内存中的数据加密。OCB模式结合了块加密和消息认证码...

    Django contrib auth authenticate函数源码解析

    django提供了一个默认的auth系统用于用户的登录和授权,并提供了一定的扩展性,允许开发者自行定义多个验证后台,每个验证后台必须实现authenticate函数,并返回None或者User对象。 默认的后台是django.contrib.auth...

    authenticate-me-project

    "authenticate-me-project" 是一个可能基于JavaScript开发的身份验证项目,主要关注用户登录和身份验证的安全性和用户体验。在Web开发中,身份验证是至关重要的,它确保只有授权的用户能够访问受保护的资源和服务。...

    authenticate-web-sdk:Haventec Authenticate的Javascript客户端库

    Haventec身份验证Web SDK 一组JavaScript函数,可促进客户端与Haventec Authenticate的集成。安装npm install @haventec/authenticate-web-sdk 一般用法authenticate-web-sdk不会作为默认导出导出,应该为每个用户...

    GoogleEarthEngine的Python3.6运行环境

    GoogleEarthEngine的Python3.6运行环境,可以直接激活使用,基于conda。放到miniconda或anaconda的安装目录的envs文件夹下,使用conda activate gee激活当前环境,输入earthengine authenticate进行下一步操作

    vueauthenticate简单的Vuejs认证库

    vue-authenticate 简单的Vue.js认证库

    m1卡感应接口函数标准版

    这两个函数分别负责发送命令到卡片和接收卡片的响应,它们通常采用ISO 14443A协议规定的帧格式。 4. **安全认证函数**:M1卡支持多种加密算法,如DES和3DES,用于保护数据的安全性。`authenticate()`函数执行认证...

Global site tag (gtag.js) - Google Analytics