Пример #1
0
        // Inherited from RoleProvider ==> Forwarded to previous provider if this provider hasn't been initialized
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            if (!InitializeCalled)
            {
                PreviousProvider.AddUsersToRoles(usernames, roleNames);
            }
            else
            {
                using (var db = ConnectToDatabase()) {
                    int        userCount = usernames.Length;
                    int        roleCount = roleNames.Length;
                    List <int> userIds   = GetUserIdsFromNames(db, usernames);
                    List <int> roleIds   = GetRoleIdsFromNames(db, roleNames);

                    // Generate a INSERT INTO for each userid/rowid combination, where userIds are the first params, and roleIds follow
                    for (int uId = 0; uId < userCount; uId++)
                    {
                        for (int rId = 0; rId < roleCount; rId++)
                        {
                            if (IsUserInRole(usernames[uId], roleNames[rId]))
                            {
                                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, WebDataResources.SimpleRoleProvder_UserAlreadyInRole, usernames[uId], roleNames[rId]));
                            }

                            // REVIEW: is there a way to batch up these inserts?
                            int rows = db.Execute("INSERT INTO " + UsersInRoleTableName + " VALUES (" + userIds[uId] + "," + roleIds[rId] + "); ");
                            if (rows != 1)
                            {
                                throw new ProviderException(WebDataResources.Security_DbFailure);
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Adds the specified user names to the specified roles for the configured applicationName.
        /// </summary>
        /// <remarks>Inherited from RoleProvider ==> Forwarded to previous provider if this provider hasn't been initialized</remarks>
        /// <param name="usernames">A string array of user names to be added to the specified roles.</param>
        /// <param name="roleNames">A string array of the role names to add the specified user names to.</param>
        /// <exception cref="System.InvalidOperationException"></exception>
        /// <exception cref="System.Configuration.Provider.ProviderException"></exception>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            if (!InitializeCalled)
            {
                PreviousProvider.AddUsersToRoles(usernames, roleNames);
            }
            else
            {
                using (var db = NewMySqlSecurityDbContext)
                {
                    int        userCount   = usernames.Length;
                    int        roleCount   = roleNames.Length;
                    List <int> userIds     = GetUserIdsFromNames(db, usernames);
                    List <int> roleIds     = GetRoleIdsFromNames(db, roleNames);
                    var        affectedRow = 0;

                    // Generate a INSERT INTO for each userid/rowid combination, where userIds are the first params, and roleIds follow
                    for (int uId = 0; uId < userCount; uId++)
                    {
                        for (int rId = 0; rId < roleCount; rId++)
                        {
                            if (IsUserInRole(usernames[uId], roleNames[rId]))
                            {
                                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.SimpleRoleProvder_UserAlreadyInRole, usernames[uId], roleNames[rId]));
                            }

                            // REVIEW: is there a way to batch up these inserts?
                            db.UsersInRoles.Add(new UsersInRoles
                            {
                                UserId = userIds[uId],
                                RoleId = roleIds[rId],
                            });
                            affectedRow++;
                        }
                    }

                    if (db.SaveChanges() != affectedRow)
                    {
                        throw new ProviderException(Resources.Security_DbFailure);
                    }
                }
            }
        }