예제 #1
0
        /// <summary>
        /// Adds the users to roles.
        /// </summary>
        /// <param name="usernames">The usernames.</param>
        /// <param name="rolenames">The rolenames.</param>
        public override void AddUsersToRoles(string[] usernames, string[] rolenames)
        {
            foreach (string rolename in rolenames)
            {
                if (String.IsNullOrEmpty(rolename))
                {
                    throw new ArgumentException(Resources.IllegalRoleName, "rolenames");
                }
                if (!RoleExists(rolename))
                {
                    throw new ProviderException(Resources.RoleNameNotFound);
                }
            }

            foreach (string username in usernames)
            {
                if (String.IsNullOrEmpty(username))
                {
                    throw new ArgumentException(Resources.IllegalUserName, "usernames");
                }
                if (username.IndexOf(',') != -1)
                {
                    throw new ArgumentException(Resources.InvalidCharactersInUserName);
                }

                foreach (string rolename in rolenames)
                {
                    if (IsUserInRole(username, rolename))
                    {
                        throw new ProviderException(Resources.UserIsAlreadyInRole);
                    }
                }
            }

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    using (MySqlConnection connection = new MySqlConnection(connectionString))
                    {
                        connection.Open();
                        MySqlCommand cmd = new MySqlCommand(
                            "INSERT INTO my_aspnet_UsersInRoles VALUES(@userId, @roleId)", connection);
                        cmd.Parameters.Add("@userId", MySqlDbType.Int32);
                        cmd.Parameters.Add("@roleId", MySqlDbType.Int32);
                        foreach (string username in usernames)
                        {
                            // either create a new user or fetch the existing user id
                            int userId = SchemaManager.CreateOrFetchUserId(connection,
                                                                           username, applicationId, true);
                            foreach (string rolename in rolenames)
                            {
                                int roleId = GetRoleId(connection, rolename);
                                cmd.Parameters[0].Value = userId;
                                cmd.Parameters[1].Value = roleId;
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }
                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "AddUsersToRoles");
                }
                throw;
            }
        }
        /// <summary>
        /// Sets the values of the specified group of property settings.
        /// </summary>
        /// <param name="context">A <see cref="T:System.Configuration.SettingsContext"/> describing the current application usage.</param>
        /// <param name="collection">A <see cref="T:System.Configuration.SettingsPropertyValueCollection"/> representing the group of property settings to set.</param>
        public override void SetPropertyValues(
            SettingsContext context, SettingsPropertyValueCollection collection)
        {
            bool   isAuthenticated = (bool)context["IsAuthenticated"];
            string username        = (string)context["UserName"];

            if (String.IsNullOrEmpty(username))
            {
                return;
            }
            if (collection.Count < 1)
            {
                return;
            }

            string index      = String.Empty;
            string stringData = String.Empty;

            byte[] binaryData = null;
            int    count      = EncodeProfileData(collection, isAuthenticated, ref index, ref stringData, ref binaryData);

            if (count < 1)
            {
                return;
            }

            MySqlTransaction txn = null;

            // save the encoded profile data to the database
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    txn = connection.BeginTransaction();

                    // either create a new user or fetch the existing user id
                    long userId = SchemaManager.CreateOrFetchUserId(connection, username,
                                                                    app.EnsureId(connection), isAuthenticated);

                    MySqlCommand cmd = new MySqlCommand(
                        @"INSERT INTO my_aspnet_profiles  
                        VALUES (@userId, @index, @stringData, @binaryData, NULL) ON DUPLICATE KEY UPDATE
                        valueindex=VALUES(valueindex), stringdata=VALUES(stringdata),
                        binarydata=VALUES(binarydata)", connection);
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@userId", userId);
                    cmd.Parameters.AddWithValue("@index", index);
                    cmd.Parameters.AddWithValue("@stringData", stringData);
                    cmd.Parameters.AddWithValue("@binaryData", binaryData);
                    count = cmd.ExecuteNonQuery();
                    if (count == 0)
                    {
                        throw new Exception(Resources.ProfileUpdateFailed);
                    }
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    if (txn != null)
                    {
                        txn.Rollback();
                    }
                    throw new ProviderException(Resources.ProfileUpdateFailed, ex);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Adds the users to the specified roles.
        /// </summary>
        /// <param name="usernames">The user names.</param>
        /// <param name="rolenames">The role names.</param>
        public override void AddUsersToRoles(string[] usernames, string[] rolenames)
        {
            if (rolenames == null || rolenames.Length == 0)
            {
                return;
            }
            if (usernames == null || usernames.Length == 0)
            {
                return;
            }

            foreach (string rolename in rolenames)
            {
                if (String.IsNullOrEmpty(rolename))
                {
                    throw new ArgumentException(Properties.Resources.IllegalRoleName, "rolenames");
                }
                if (!RoleExists(rolename))
                {
                    throw new ProviderException(Properties.Resources.RoleNameNotFound);
                }
            }

            foreach (string username in usernames)
            {
                if (String.IsNullOrEmpty(username))
                {
                    throw new ArgumentException(Properties.Resources.IllegalUserName, "usernames");
                }
                if (username.IndexOf(',') != -1)
                {
                    throw new ArgumentException(Properties.Resources.InvalidCharactersInUserName);
                }

                foreach (string rolename in rolenames)
                {
                    if (IsUserInRole(username, rolename))
                    {
                        throw new ProviderException(Properties.Resources.UserIsAlreadyInRole);
                    }
                }
            }

            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                MySqlTransaction txn = null;
                try
                {
                    connection.Open();
                    txn = connection.BeginTransaction();
                    MySqlCommand cmd = new MySqlCommand(
                        "INSERT INTO my_aspnet_usersinroles VALUES(@userId, @roleId)", connection);
                    cmd.Parameters.Add("@userId", MySqlDbType.Int32);
                    cmd.Parameters.Add("@roleId", MySqlDbType.Int32);
                    foreach (string username in usernames)
                    {
                        // either create a new user or fetch the existing user id
                        long userId = SchemaManager.CreateOrFetchUserId(connection,
                                                                        username, app.FetchId(connection), true);
                        foreach (string rolename in rolenames)
                        {
                            int roleId = GetRoleId(connection, rolename);
                            cmd.Parameters[0].Value = userId;
                            cmd.Parameters[1].Value = roleId;
                            cmd.ExecuteNonQuery();
                        }
                    }
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    if (txn != null)
                    {
                        txn.Rollback();
                    }
                    if (WriteExceptionsToEventLog)
                    {
                        WriteToEventLog(ex, "AddUsersToRoles");
                    }
                    throw;
                }
            }
        }