|
|
|
Joined: 6/12/2007
Posts: 31
|
|
|
Ykhan
No problem, the source of the stored procedure below - the site in question had a series of sub-sites one per film hence the naming used here but essentially this sproc takes a UserID, PortalName and RoleName as input and ensures that the user is created in that portal and attached to that role.
if you need any further help with it then give me a yell.
Kevin
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
/************************************************************
Title: MMF_UpdateUserFilmAccess
Author: Kevin Crampton
Created: 19th July 2007
Last Modified: 30th July 2007
Purpose: When a user is granted access to a particular
film on the MMF site we also need to grant them
access to that film's subsite. This stored
procedure wraps this up in one call.
************************************************************/
CREATE
PROCEDURE[dbo].[MMF_UpdateUserFilmAccess]
(
@UserID
int,
@PortalName
nvarchar(128),
@RoleName
)
nvarchar(50)
AS
BEGIN
-- Wrap processing in a transaction
BEGIN TRANSACTION
-- Local variables with defaults
DECLARE @PortalID int
DECLARE @RoleID int
SET @PortalID = 0SET @RoleID = 0-- Based on the portal name, work out the portal ID
SELECT @PortalID = PortalIdFROM PortalsWHERE PortalName LIKE @PortalName-- Check that a record was returned
IF @PortalID = 0 GOTO ERROR_HANDLER-- Make sure that the user isn't already assigned into this
-- portal
DECLARE @UserPortalCount int
SELECT @UserPortalCount = ISNULL(COUNT(UserPortalID), 0)
FROM UserPortalsWHERE
UserId
= @UserID AND
PortalId
= @PortalID-- Insert the user if they do not already exist in that portal
IF @UserPortalCount = 0BEGIN
-- Assign the user into that child portal
INSERT INTO UserPortals(
UserId
,
PortalId
,
CreatedDate
,
Authorised
)
VALUES
(
@UserID
,
@PortalID
,
GETDATE(),
1
)
-- Check for errors
IF @@Error != 0 GOTO ERROR_HANDLER-- Now work out the role in that portal into which we
-- wish to assign the user
SELECT @RoleID = RoleIdFROM RolesWHERE PortalId = @PortalID AND
RoleName
LIKE @RoleName-- Check that a record was returned
IF @RoleID = 0 GOTO ERROR_HANDLER-- Insert the user into that role
INSERT INTO UserRoles(
UserId
,
RoleId
,
EffectiveDate
)
VALUES
(
@UserID
,
@RoleID
,
GETDATE()
)
-- Check for errors
IF @@Error != 0 GOTO ERROR_HANDLEREND
-- Commit the transaction if we have got this far
COMMIT TRANSACTION
ERROR_HANDLER
GOTO COMPLETE_OK:
ROLLBACK TRANSACTION
COMPLETE_OK
:
END
SET NOCOUNT OFF
|
|
|
|
| |
|
|
|
Joined: 9/18/2006
Posts: 308
|
|
|
This is an older post but I figured for all the solutions I've been given I'd give back.
For our application, we have junior sales reps who we want to be able to create child portals for clients without being in a Super User role. However, we don't want them to have admin control, just their bosses get this. So the sequence is, create the portal, get the UserID of the Sales Rep's boss, make them the admin and in the Create Portal routine we make that senior user the user admin.
All well and good but now the Sales Rep needs access too! No problem, he's the logged in user so we do this:
if(UserInfo.IsInRole("Sales Rep"))
{
UserInfo salesRep = new UserInfo(); <-- Note: DotNetNuke UserInfo is same case as ...
salesRep.FirstName = UserInfo.FirstName; <-- PortalModuleBase UserInfo ... different critters, same name !!! -- No context sensitive highlighting here makes this a bit hard to absorb.
salesRep.LastName = UserInfo.LastName;
salesRep.DisplayName = UserInfo.DisplayName;
salesRep.Email = UserInfo.Email;
salesRep.PortalID = intPortalId;
salesRep.Membership.Password = UserInfo.Membership.Password;
salesRep.Membership.PasswordConfirm = UserInfo.Membership.Password;
if (UserController.CreateUser(ref salesRep) == UserCreateStatus.Success)
{
RoleController rc = new RoleController();
RoleInfo ri = rc.GetRoleByName(intPortalId, "RegisteredUsers");
rc.AddUserRole(intPortalId, salesRep.UserID, ri.RoleID, Null.NullDate);
}
}
Testing for the Sales Rep role, when I'm logged in as Host or Admin and test other code, this is not fired. What I'm looking up now is how to just create the role of Sales Rep in the child portal and then add the currently logged in sales rep to it, etc. I don't see the method right now but it should be straightforward.
If anyone needs the code to do a similar action I'll share it. Piecing it together from the different forum posts is very easy however. I'm quite thankful I'm not the first person down this path. :)
|
|
|
|
| |
|
|
|
Joined: 9/18/2006
Posts: 308
|
|
|
WHOOPS! :) Forgot I need to test this part of the code. This method of passing by ref is new but it returns the new sales rep object. It isn't a Boolean.
if (UserController.CreateUser(ref salesRep) == UserCreateStatus.Success) <-- so this needs to be resolved.
But the idea is on the right track. I'll post the solution once I resolve this point and other points.
|
|
|
|
| |
|
|
|
Joined: 9/18/2006
Posts: 308
|
|
|
As promised, here is the code for adding in users to child portals. I'm showing the entire part of the method I'm working with, which will make things clearer.
A note here -- the only reason a user won't be added to a child portal is required fields in the User Info, Membership or Profile not being correct or a NULL is getting added. The most common case of this is getting the password for the User without using methods in the Membership Property of the User Info. I'll include this as well.
BTW, whoever had the patience to do this with the stored procedure is a very meticulous person. I prefer the lazy way, myself ... :)
Here is the code:
//Create Portal if (String.IsNullOrEmpty(message)) { //Attempt to create the portal UserInfo adminMasterRep = new UserInfo(); int intPortalId; /* * Set up the site so that any Master Sales Rep created / deleted is automatically added/removed from the Master * Sales Rep -- Sales Rep table. There should just be a text box and an Update button for the Super User page in the * site to list out who is deleted, who is added. */
try { adminMasterRep = new UserInfo { FirstName = masterRepInfo.FirstName, LastName = masterRepInfo.LastName, Username = masterRepInfo.Username, DisplayName = masterRepInfo.FirstName + " " + masterRepInfo.LastName, Email = masterRepInfo.Email, IsSuperUser = false, Membership = { Approved = true, Password = masterRepAdminUser.GetPassword(), PasswordConfirm = masterRepAdminUser.GetPassword(), PasswordQuestion = masterRepInfo.Membership.PasswordQuestion, PasswordAnswer = masterRepInfo.Membership.PasswordAnswer }, Profile = { FirstName = masterRepInfo.Profile.FirstName, LastName = masterRepInfo.Profile.LastName } };
intPortalId = objPortalController.CreatePortal(txtPortalName.Text, adminMasterRep, txtDescription.Text, txtKeyWords.Text, template, homeDir, strPortalAlias, strServerPath, strChildPath, blnChild);
//Add the sales rep as a registered user for the new portal if (UserInfo.IsInRole("Sales Reps")) { UserInfo salesRep = new UserInfo { FirstName = UserInfo.FirstName, LastName = UserInfo.LastName, DisplayName = UserInfo.DisplayName, Email = UserInfo.Email, PortalID = intPortalId, IsSuperUser = false, Membership = { // Further up in the code above this part of the method lives this: //Creating object of MembershipUser class which will take username as parameter. //This is the only way to get the password for the admin user. // MembershipUser masterRepAdminUser = Membership.GetUser(masterRepInfo.Username); // gets the password for the Sales Rep's boss ... // MembershipUser loggedInSalesRep = Membership.GetUser(UserInfo.Username); // gets the password info for the logged in rep, as below. Username = UserInfo.Username, Password = loggedInSalesRep.GetPassword(), PasswordConfirm = loggedInSalesRep.GetPassword() }, Profile = { FirstName = UserInfo.Profile.FirstName, LastName = UserInfo.Profile.LastName, Cell = UserInfo.Profile.Cell, Telephone = UserInfo.Profile.Telephone, IM = UserInfo.Profile.IM } };
UserController.CreateUser(ref salesRep);
if (salesRep.UserID > 0) { RoleController rc = new RoleController(); RoleInfo ri = rc.GetRoleByName(intPortalId, "Registered Users"); rc.AddUserRole(intPortalId, salesRep.UserID, ri.RoleID, DateTime.Now.AddDays(365)); } }
|
|
|
|
| |