forked from ashishpd/DnnSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulkUserCreation.sql
446 lines (408 loc) · 17.4 KB
/
BulkUserCreation.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
--Script created by Ash Prasad
--Script can be used to create users in bulk in DotNetNuke
--No license - free to use as you feel :)
--PLEASE DO NOT USE THIS IN PRODUCTION. ONLY MEANT TO BE USED IN TEST ENVIRONMENT
--How it works. Very simple. Reads settings for 'admin' account and simply copies that over to new user
-- Very simple to change though, just change UserToCopy value to something else
--What's going to be the password for new users. Same as that of 'admin'.
--Replace dnn_ with empty, space or something else if you don't use standard database prefix of 'dnn_'
--User name will be user1, user2, user3, etc,
--Email will be [email protected], [email protected]
--FirstName will be First1, First2, First3, etc.
--LastName will be Last1, Last2, Last3, etc.
--DisplayName will be First1 Last1, First2, Last2, etc.
--Tweak with @StartingUserID (default 10) and or @UsersToCreate (default 1000) to change starting userid
-- and total users to create
/** BEGIN Configuration **/
DECLARE @UserToCopy NVARCHAR(100)
DECLARE @RoleMemberships NVARCHAR(1000)
DECLARE @StartingUserID INT
DECLARE @UsersToCreate INT
DECLARE @PortalID INT = 0
--Feel free to change user from 'admin' to 'something else'
SET @UserToCopy = 'testuser'
-- RoleMemberships to add to user. No need to add "Registered Users" as this will always be added.
-- Can be left empty for no extra role memberships
SET @RoleMemberships = 'Testrole1,Testrole2'
-- bump it to number where you want to start username from
SET @StartingUserID = 100000
-- set it number of users you want to create
SET @UsersToCreate = 350000
/** END Configuration **/
DECLARE @UserName NVARCHAR(100)
DECLARE @Password NVARCHAR(1000)
DECLARE @Email NVARCHAR(100)
DECLARE @PasswordSalt NVARCHAR(1000)
DECLARE @ApplicationId NVARCHAR(1000)
DECLARE @RoleID INT
DECLARE @AspUserID UNIQUEIDENTIFIER
DECLARE @CurrentDate AS DATETIME = GETUTCDATE()
DECLARE @FirstName NVARCHAR(100)
DECLARE @LastName NVARCHAR(100)
DECLARE @DisplayName NVARCHAR(100)
DECLARE @UserID INT
DECLARE @Counter INT
DECLARE @UserSuffix NVARCHAR(25)
DECLARE @UserIdCopy INT
DECLARE @ProfileProperty_FirstName INT
DECLARE @ProfileProperty_LastName INT
DECLARE @ProfileProperty_PreferredTimeZone INT
DECLARE @ProfileProperty_PreferredLocale INT
DECLARE @PreferredTimezone NVARCHAR(100)
DECLARE @PreferredLocale NVARCHAR(10)
DECLARE @RoleMembershipsCopy NVARCHAR(1000)
DECLARE @SingleRole NVARCHAR(100)
DECLARE @NewRoleID INT
SELECT @ProfileProperty_FirstName = PropertyDefinitionId
FROM dbo.dnn_ProfilePropertyDefinition
WHERE PortalID = @PortalID
AND PropertyName = 'FirstName'
SELECT @ProfileProperty_LastName = PropertyDefinitionId
FROM dbo.dnn_ProfilePropertyDefinition
WHERE PortalID = @PortalID
AND PropertyName = 'LastName'
SELECT @ProfileProperty_PreferredTimeZone = PropertyDefinitionId
FROM dbo.dnn_ProfilePropertyDefinition
WHERE PortalID = @PortalID
AND PropertyName = 'PreferredTimeZone'
SELECT @ProfileProperty_PreferredLocale = PropertyDefinitionId
FROM dbo.dnn_ProfilePropertyDefinition
WHERE PortalID = @PortalID
AND PropertyName = 'PreferredLocale'
SELECT @Password = [Password] ,
@PasswordSalt = PasswordSalt ,
@ApplicationId = dbo.aspnet_Membership.ApplicationId
FROM dbo.aspnet_Membership
INNER JOIN dbo.aspnet_Users ON dbo.aspnet_Membership.UserId = dbo.aspnet_Users.UserId
WHERE UserName = @UserToCopy
SELECT @UserIdCopy = UserID
FROM dbo.dnn_Users
WHERE Username = @UserToCopy
SELECT @PreferredTimezone = PropertyValue
FROM dbo.dnn_UserProfile
WHERE PropertyDefinitionID = @ProfileProperty_PreferredTimeZone
AND UserID = @UserIdCopy
SELECT @PreferredLocale = PropertyValue
FROM dbo.dnn_UserProfile
WHERE PropertyDefinitionID = @ProfileProperty_PreferredLocale
AND UserID = @UserIdCopy
IF @ApplicationId IS NULL
BEGIN
RAISERROR ('User does not exist', 16, 1);
RETURN
END
SELECT @RoleID = [RoleID]
FROM dbo.dnn_Roles
WHERE RoleName = 'Registered Users'
AND PortalID = @PortalID
SET @Counter = 0
WHILE @Counter < @UsersToCreate
BEGIN
SET @Counter = @Counter + 1
SET @AspUserID = NEWID()
SET @UserSuffix = CONVERT(NVARCHAR(25), @StartingUserID + @Counter)
SET @UserName = 'user' + @UserSuffix
SET @Email = 'user' + @UserSuffix + '@email.com'
SET @FirstName = 'First' + @UserSuffix
SET @LastName = 'Last' + @UserSuffix
SET @DisplayName = @FirstName + ' ' + @LastName
INSERT INTO dbo.aspnet_Users
( ApplicationId ,
UserId ,
UserName ,
LoweredUserName ,
MobileAlias ,
IsAnonymous ,
LastActivityDate
)
VALUES ( @ApplicationId , -- ApplicationId - uniqueidentifier
@AspUserID , -- UserId - uniqueidentifier
@UserName , -- UserName - nvarchar(256)
@UserName , -- LoweredUserName - nvarchar(256)
NULL , -- MobileAlias - nvarchar(16)
0 , -- IsAnonymous - bit
@CurrentDate -- LastActivityDate - datetime
)
INSERT INTO dbo.aspnet_Membership
( ApplicationId ,
UserId ,
Password ,
PasswordFormat ,
PasswordSalt ,
MobilePIN ,
Email ,
LoweredEmail ,
PasswordQuestion ,
PasswordAnswer ,
IsApproved ,
IsLockedOut ,
CreateDate ,
LastLoginDate ,
LastPasswordChangedDate ,
LastLockoutDate ,
FailedPasswordAttemptCount ,
FailedPasswordAttemptWindowStart ,
FailedPasswordAnswerAttemptCount ,
FailedPasswordAnswerAttemptWindowStart ,
Comment
)
VALUES ( @ApplicationId , -- ApplicationId - uniqueidentifier
@AspUserID , -- UserId - uniqueidentifier
@Password , -- Password - nvarchar(128)
2 , -- PasswordFormat - int
@PasswordSalt , -- PasswordSalt - nvarchar(128)
NULL , -- MobilePIN - nvarchar(16)
@Email , -- Email - nvarchar(256)
@Email , -- LoweredEmail - nvarchar(256)
NULL , -- PasswordQuestion - nvarchar(256)
NULL , -- PasswordAnswer - nvarchar(128)
1 , -- IsApproved - bit
0 , -- IsLockedOut - bit
@CurrentDate , -- CreateDate - datetime
@CurrentDate , -- LastLoginDate - datetime
@CurrentDate , -- LastPasswordChangedDate - datetime
'1754-01-01 00:00:00.000' , -- LastLockoutDate - datetime
0 , -- FailedPasswordAttemptCount - int
'1754-01-01 00:00:00.000' , -- FailedPasswordAttemptWindowStart - datetime
0 , -- FailedPasswordAnswerAttemptCount - int
'1754-01-01 00:00:00.000' , -- FailedPasswordAnswerAttemptWindowStart - datetime
NULL -- Comment - ntext
)
INSERT INTO dbo.dnn_Users
( Username ,
FirstName ,
LastName ,
IsSuperUser ,
AffiliateId ,
Email ,
DisplayName ,
UpdatePassword ,
LastIPAddress ,
IsDeleted ,
CreatedByUserID ,
CreatedOnDate ,
LastModifiedByUserID ,
LastModifiedOnDate
)
VALUES ( @UserName , -- Username - nvarchar(100)
@FirstName , -- FirstName - nvarchar(50)
@LastName , -- LastName - nvarchar(50)
0 , -- IsSuperUser - bit
NULL , -- AffiliateId - int
@Email , -- Email - nvarchar(256)
@DisplayName , -- DisplayName - nvarchar(128)
0 , -- UpdatePassword - bit
N'' , -- LastIPAddress - nvarchar(50)
0 , -- IsDeleted - bit
0 , -- CreatedByUserID - int
@CurrentDate , -- CreatedOnDate - datetime
0 , -- LastModifiedByUserID - int
@CurrentDate -- LastModifiedOnDate - datetime
)
SET @UserID = SCOPE_IDENTITY()
INSERT INTO dbo.dnn_UserRoles
( UserID ,
RoleID ,
ExpiryDate ,
IsTrialUsed ,
EffectiveDate ,
CreatedByUserID ,
CreatedOnDate ,
LastModifiedByUserID ,
LastModifiedOnDate,
Status,
IsOwner
)
VALUES ( @UserID , -- UserID - int
@RoleID , -- RoleID - int
NULL , -- ExpiryDate - datetime
1 , -- IsTrialUsed - bit
NULL , -- EffectiveDate - datetime
0 , -- CreatedByUserID - int
@CurrentDate , -- CreatedOnDate - datetime
0 , -- LastModifiedByUserID - int
@CurrentDate, -- LastModifiedOnDate - datetime
1,
0
)
INSERT INTO dbo.dnn_UserPortals
( UserId ,
PortalId ,
CreatedDate ,
Authorised ,
IsDeleted ,
RefreshRoles
)
VALUES ( @UserID , -- UserId - int
@PortalID , -- PortalId - int
@CurrentDate , -- CreatedDate - datetime
1 , -- Authorised - bit
0 , -- IsDeleted - bit
0 -- RefreshRoles - bit
)
--Create FirstName UserProfile Property
INSERT INTO dbo.dnn_UserProfile
( UserID ,
PropertyDefinitionID ,
PropertyValue ,
PropertyText ,
Visibility ,
LastUpdatedDate ,
ExtendedVisibility
)
VALUES ( @UserID , -- UserID - int
@ProfileProperty_FirstName , -- PropertyDefinitionID - int
@FirstName , -- PropertyValue - nvarchar(3750)
NULL , -- PropertyText - nvarchar(max)
0 , -- Visibility - int
@CurrentDate , -- LastUpdatedDate - datetime
'' -- ExtendedVisibility - varchar(400)
)
--Create LastName UserProfile Property
INSERT INTO dbo.dnn_UserProfile
( UserID ,
PropertyDefinitionID ,
PropertyValue ,
PropertyText ,
Visibility ,
LastUpdatedDate ,
ExtendedVisibility
)
VALUES ( @UserID , -- UserID - int
@ProfileProperty_LastName , -- PropertyDefinitionID - int
@LastName , -- PropertyValue - nvarchar(3750)
NULL , -- PropertyText - nvarchar(max)
0 , -- Visibility - int
@CurrentDate , -- LastUpdatedDate - datetime
'' -- ExtendedVisibility - varchar(400)
)
--Create PreferredTimeZone UserProfile Property
INSERT INTO dbo.dnn_UserProfile
( UserID ,
PropertyDefinitionID ,
PropertyValue ,
PropertyText ,
Visibility ,
LastUpdatedDate ,
ExtendedVisibility
)
VALUES ( @UserID , -- UserID - int
@ProfileProperty_PreferredTimeZone , -- PropertyDefinitionID - int
@PreferredTimezone , -- PropertyValue - nvarchar(3750)
NULL , -- PropertyText - nvarchar(max)
0 , -- Visibility - int
@CurrentDate , -- LastUpdatedDate - datetime
'' -- ExtendedVisibility - varchar(400)
)
--Create LastName UserProfile Property
INSERT INTO dbo.dnn_UserProfile
( UserID ,
PropertyDefinitionID ,
PropertyValue ,
PropertyText ,
Visibility ,
LastUpdatedDate ,
ExtendedVisibility
)
VALUES ( @UserID , -- UserID - int
@ProfileProperty_PreferredLocale , -- PropertyDefinitionID - int
@PreferredLocale , -- PropertyValue - nvarchar(3750)
NULL , -- PropertyText - nvarchar(max)
0 , -- Visibility - int
@CurrentDate , -- LastUpdatedDate - datetime
'' -- ExtendedVisibility - varchar(400)
)
-- create and add to roles
SET @RoleMembershipsCopy = @RoleMemberships
WHILE LEN(@RoleMembershipsCopy) > 0
BEGIN
-- parse roles create
IF PATINDEX('%,%', @RoleMembershipsCopy) > 0
BEGIN
SET @SingleRole = SUBSTRING(@RoleMembershipsCopy, 0,
PATINDEX('%,%',
@RoleMembershipsCopy))
--SELECT @SingleRole
SET @RoleMembershipsCopy = SUBSTRING(@RoleMembershipsCopy,
LEN(@SingleRole
+ ',') + 1,
LEN(@RoleMembershipsCopy))
END
ELSE
BEGIN
SET @SingleRole = @RoleMembershipsCopy
SET @RoleMembershipsCopy = NULL
--SELECT @SingleRole
END
SET @SingleRole = LTRIM(RTRIM(@SingleRole))
-- create roles if necessary
IF NOT EXISTS ( SELECT *
FROM dbo.dnn_Roles
WHERE ( PortalID = @PortalID )
AND ( RoleName = @SingleRole ) )
BEGIN
INSERT INTO [dbo].[dnn_Roles]
( [PortalID] ,
[RoleName] ,
[IsPublic] ,
[AutoAssignment] ,
[CreatedByUserID] ,
[CreatedOnDate] ,
[LastModifiedByUserID] ,
[LastModifiedOnDate] ,
[Status] ,
[SecurityMode] ,
[IsSystemRole]
)
VALUES ( @PortalID ,
@SingleRole ,
0 ,
0 ,
-1 ,
@CurrentDate ,
-1 ,
@CurrentDate ,
1 ,
0 ,
0
)
SELECT @NewRoleID = SCOPE_IDENTITY()
END
ELSE
BEGIN
SELECT @NewRoleID = RoleID
FROM dnn_Roles
WHERE ( PortalID = @PortalID )
AND ( RoleName = @SingleRole )
END
INSERT INTO dbo.dnn_UserRoles
( UserID ,
RoleID ,
ExpiryDate ,
IsTrialUsed ,
EffectiveDate ,
CreatedByUserID ,
CreatedOnDate ,
LastModifiedByUserID ,
LastModifiedOnDate
)
VALUES ( @UserID , -- UserID - int
@NewRoleID , -- RoleID - int
NULL , -- ExpiryDate - datetime
1 , -- IsTrialUsed - bit
NULL , -- EffectiveDate - datetime
0 , -- CreatedByUserID - int
@CurrentDate , -- CreatedOnDate - datetime
0 , -- LastModifiedByUserID - int
@CurrentDate -- LastModifiedOnDate - datetime
)
END --WHILE LEN(@RoleMembershipsCopy) > 0
END
--Following SQLs can be used to verify new user records
/*
SELECT * FROM dbo.aspnet_Users
SELECT * FROM dbo.aspnet_Membership
SELECT * FROM dbo.dnn_users
SELECT * FROM dbo.dnn_UserRoles
SELECT * FROM dbo.dnn_UserPortals
*/