由于不是全新开发,而是在现在有程序补充功能,Insus.NET不想在程序作过多更改,只好修改存储过程来实现它们。现下面把实现上图highlight的选项功能分享出来。
保持密码原有记录值,只要管理员设置了密码策略之后,用户更改自己的密码时,就会应用到了新密码策略。
首先写一个获取参数值的自定义函数:
udf_GetPasswordParameterValue SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Insus.NET
-- Create date: 2012-02-05
-- Description: Get Password Parameter Value
-- =============================================
CREATE FUNCTION [dbo].[udf_GetPasswordParameterValue]
(
@PasswordParameterId TINYINT
)
RETURNS TINYINT
AS
BEGIN
DECLARE @ReturnValue TINYINT
SELECT @ReturnValue = [ParameterValue] FROM [dbo].[PasswordParameter] WHERE [PasswordParameterId] = @PasswordParameterId
Return @ReturnValue
END
GO
接下来,Insus.NET要修改有更改密码的的存储过程:
usp_Users_UpdatePassword SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Insus.NET
-- Create date: 2008-12-26
-- Update date: 2012-06-05 23:11:19
-- Description: Update User Password
-- =============================================
ALTER PROCEDURE [dbo].[usp_Users_UpdatePassword]
(
@ID INT,
@OldPassword nvarchar(30),
@Password nvarchar(30)
)
AS
--判断用户输入的密码是否正确
IF NOT EXISTS(SELECT TOP 1 * FROM [Users] WHERE [Password] = @OldPassword AND [UsersId] = @ID)
BEGIN
RAISERROR(N'旧密码输入不正确,无法更新!',16,1)
RETURN
END
--下面两个变量,是从密码策略表获取最小长度与最大长度值
DECLARE @PasswordLengthMin TINYINT = [dbo].[udf_GetPasswordParameterValue](3)
DECLARE @PasswordLengthMax TINYINT = [dbo].[udf_GetPasswordParameterValue](4)
--如果大于0,说明设置了密码最小长度策略
IF @PasswordLengthMin > 0 AND LEN(@Password) < @PasswordLengthMin
BEGIN
RAISERROR(N'密码最小长度必须大于或等于%d字符。',16,1,@PasswordLengthMin)
RETURN
END
--如果大于0,说明设置了密码最大长度策略
IF @PasswordLengthMax > 0 AND LEN(@Password) > @PasswordLengthMax
BEGIN
RAISERROR(N'密码最大长度必须小于或等于%d字符。',16,1,@PasswordLengthMax)
RETURN
END
--更新密码
UPDATE [Users] SET [Password] = @Password WHERE [UsersId] = @ID