ALTER PROCEDURE dbo.aspnet_UsersInRoles_AddUsersToRoles --添加用户到角色
@ApplicationName NVARCHAR(256),
@UserNames NVARCHAR(4000),
@RoleNames NVARCHAR(4000),
@TimeZoneAdjustment INT
AS
BEGIN
DECLARE @ApplicationId UNIQUEIDENTIFIER
SELECT @ApplicationId = NULL
SELECT @ApplicationId = ApplicationId FROM aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
IF (@ApplicationId IS NULL)
RETURN(2)
/*返回@ApplicationId,此处不同的是返回2*/
DECLARE @TranStarted BIT
DECLARE @ErrorCode INT
SET @ErrorCode = 0
SET @TranStarted = 0
IF( @@TRANCOUNT = 0 ) --如果当前活动事务为0,开始事务并设置事务参数为1
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
DECLARE @RoleId UNIQUEIDENTIFIER
DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @UserName NVARCHAR(256)
DECLARE @RoleName NVARCHAR(256)
DECLARE @CurrentPosU INT
DECLARE @NextPosU INT
DECLARE @CurrentPosR INT
DECLARE @NextPosR INT
/*声明关于字符的又来了*/
SELECT @CurrentPosU = 1
WHILE(@CurrentPosU <= LEN(@UserNames))
BEGIN
SELECT @NextPosU = CHARINDEX(N',', @UserNames, @CurrentPosU)
IF (@NextPosU = 0 OR @NextPosU IS NULL)
SELECT @NextPosU = LEN(@UserNames) + 1
SELECT @UserName = SUBSTRING(@UserNames, @CurrentPosU, @NextPosU - @CurrentPosU)
SELECT @CurrentPosU = @NextPosU+1
SELECT @CurrentPosR = 1
WHILE(@CurrentPosR <= LEN(@RoleNames))
BEGIN
SELECT @NextPosR = CHARINDEX(N',', @RoleNames, @CurrentPosR)
IF (@NextPosR = 0 OR @NextPosR IS NULL)
SELECT @NextPosR = LEN(@RoleNames) + 1
SELECT @RoleName = SUBSTRING(@RoleNames, @CurrentPosR, @NextPosR - @CurrentPosR)
SELECT @CurrentPosR = @NextPosR+1
/*对用户名和角色名进行我现在还看不明白的操作,不明白意思*/
SELECT @RoleId = NULL
SELECT @RoleId = RoleId FROM dbo.aspnet_Roles WHERE LoweredRoleName = LOWER(@RoleName) AND ApplicationId = @ApplicationId
----判断角色是否存在
IF (@RoleId IS NULL)
BEGIN
SELECT @RoleName --不存在则回滚.
SET @ErrorCode = 2
GOTO Cleanup
END
SELECT @UserId = NULL
SELECT @UserId = UserId FROM dbo.aspnet_Users WHERE LoweredUserName = LOWER(@UserName) AND ApplicationId = @ApplicationId
---判断用户是否存在
IF (@UserId IS NULL)
BEGIN ---不存在则获取当前时间并添加此用户
DECLARE @DateTimeNowUTC DATETIME
EXEC dbo.aspnet_GetUtcDate @TimeZoneAdjustment, @DateTimeNowUTC OUTPUT
EXEC dbo.aspnet_Users_CreateUser @ApplicationId, @UserName, 0, @DateTimeNowUTC, @UserId OUTPUT
END
IF (EXISTS(SELECT * FROM dbo.aspnet_UsersInRoles WHERE @UserId = UserId AND @RoleId = RoleId))
BEGIN --如果此用户在此角色中已存在,则回滚
SELECT @UserName, @RoleName
SET @ErrorCode = 3
GOTO Cleanup
END
INSERT INTO dbo.aspnet_UsersInRoles (UserId, RoleId) VALUES(@UserId, @RoleId)
-----如果角色存在且用户也存在再加上此用户不存在此角色中,则添加到此角色中
END
END
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION ---结束事务
END
RETURN(0)
Cleanup:
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
END
@ApplicationName NVARCHAR(256),
@UserNames NVARCHAR(4000),
@RoleNames NVARCHAR(4000),
@TimeZoneAdjustment INT
AS
BEGIN
DECLARE @ApplicationId UNIQUEIDENTIFIER
SELECT @ApplicationId = NULL
SELECT @ApplicationId = ApplicationId FROM aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
IF (@ApplicationId IS NULL)
RETURN(2)
/*返回@ApplicationId,此处不同的是返回2*/
DECLARE @TranStarted BIT
DECLARE @ErrorCode INT
SET @ErrorCode = 0
SET @TranStarted = 0
IF( @@TRANCOUNT = 0 ) --如果当前活动事务为0,开始事务并设置事务参数为1
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END
ELSE
SET @TranStarted = 0
DECLARE @RoleId UNIQUEIDENTIFIER
DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @UserName NVARCHAR(256)
DECLARE @RoleName NVARCHAR(256)
DECLARE @CurrentPosU INT
DECLARE @NextPosU INT
DECLARE @CurrentPosR INT
DECLARE @NextPosR INT
/*声明关于字符的又来了*/
SELECT @CurrentPosU = 1
WHILE(@CurrentPosU <= LEN(@UserNames))
BEGIN
SELECT @NextPosU = CHARINDEX(N',', @UserNames, @CurrentPosU)
IF (@NextPosU = 0 OR @NextPosU IS NULL)
SELECT @NextPosU = LEN(@UserNames) + 1
SELECT @UserName = SUBSTRING(@UserNames, @CurrentPosU, @NextPosU - @CurrentPosU)
SELECT @CurrentPosU = @NextPosU+1
SELECT @CurrentPosR = 1
WHILE(@CurrentPosR <= LEN(@RoleNames))
BEGIN
SELECT @NextPosR = CHARINDEX(N',', @RoleNames, @CurrentPosR)
IF (@NextPosR = 0 OR @NextPosR IS NULL)
SELECT @NextPosR = LEN(@RoleNames) + 1
SELECT @RoleName = SUBSTRING(@RoleNames, @CurrentPosR, @NextPosR - @CurrentPosR)
SELECT @CurrentPosR = @NextPosR+1
/*对用户名和角色名进行我现在还看不明白的操作,不明白意思*/
SELECT @RoleId = NULL
SELECT @RoleId = RoleId FROM dbo.aspnet_Roles WHERE LoweredRoleName = LOWER(@RoleName) AND ApplicationId = @ApplicationId
----判断角色是否存在
IF (@RoleId IS NULL)
BEGIN
SELECT @RoleName --不存在则回滚.
SET @ErrorCode = 2
GOTO Cleanup
END
SELECT @UserId = NULL
SELECT @UserId = UserId FROM dbo.aspnet_Users WHERE LoweredUserName = LOWER(@UserName) AND ApplicationId = @ApplicationId
---判断用户是否存在
IF (@UserId IS NULL)
BEGIN ---不存在则获取当前时间并添加此用户
DECLARE @DateTimeNowUTC DATETIME
EXEC dbo.aspnet_GetUtcDate @TimeZoneAdjustment, @DateTimeNowUTC OUTPUT
EXEC dbo.aspnet_Users_CreateUser @ApplicationId, @UserName, 0, @DateTimeNowUTC, @UserId OUTPUT
END
IF (EXISTS(SELECT * FROM dbo.aspnet_UsersInRoles WHERE @UserId = UserId AND @RoleId = RoleId))
BEGIN --如果此用户在此角色中已存在,则回滚
SELECT @UserName, @RoleName
SET @ErrorCode = 3
GOTO Cleanup
END
INSERT INTO dbo.aspnet_UsersInRoles (UserId, RoleId) VALUES(@UserId, @RoleId)
-----如果角色存在且用户也存在再加上此用户不存在此角色中,则添加到此角色中
END
END
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION ---结束事务
END
RETURN(0)
Cleanup:
IF( @TranStarted = 1 )
BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
END