Example #1
0
    protected void btnCreateStore_Click(object sender, EventArgs e)
    {
        IAzManStorage storage = new SqlAzManStorage(ConfigurationManager.ConnectionStrings["NetSqlAzManStorage"].ConnectionString);

        storage.OpenConnection();
        try
        {
            storage.BeginTransaction(AzManIsolationLevel.ReadUncommitted);
            IAzManStore store = storage.CreateStore("Store Created Programmatically", "store description");
            for (int i = 0; i < 10; i++)
            {
                IAzManApplication app      = store.CreateApplication("App " + i.ToString(), "application description");
                IAzManItem        prevItem = null;
                for (int j = 0; j < 10; j++)
                {
                    IAzManItem item = app.CreateItem("Item " + j.ToString(), "item description", ItemType.Operation);
                    if (prevItem != null)
                    {
                        item.AddMember(prevItem);
                    }
                    prevItem = item;
                }
            }
            storage.CommitTransaction();
        }
        catch
        {
            storage.RollBackTransaction();
            throw;
        }
        finally
        {
            storage.CloseConnection();
        }
    }
        /// <summary>
        /// Adds the specified user names to the specified roles for the configured applicationName.
        /// </summary>
        /// <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>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            using (IAzManStorage storage = new SqlAzManStorage(this.storageCache.ConnectionString))
            {
                try
                {
                    storage.OpenConnection();
                    storage.BeginTransaction();
                    IAzManApplication application = storage[this.storeName][this.applicationName];
                    foreach (string roleName in roleNames)
                    {
                        IAzManItem role = application.GetItem(roleName);
                        if (role.ItemType != ItemType.Role)
                        {
                            throw new ArgumentException(String.Format("{0} must be a Role.", roleName));
                        }

                        foreach (string username in usernames)
                        {
                            IAzManSid    owner        = new SqlAzManSID(((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent()).User);
                            WhereDefined whereDefined = WhereDefined.LDAP;
                            if (this.userLookupType == "LDAP")
                            {
                                string    fqun      = this.getFQUN(username);
                                NTAccount ntaccount = new NTAccount(fqun);
                                if (ntaccount == null)
                                {
                                    throw SqlAzManException.UserNotFoundException(username, null);
                                }
                                IAzManSid sid = new SqlAzManSID(((SecurityIdentifier)(ntaccount.Translate(typeof(SecurityIdentifier)))));
                                if (sid == null)
                                {
                                    throw SqlAzManException.UserNotFoundException(username, null);
                                }
                                role.CreateAuthorization(owner, whereDefined, sid, WhereDefined.LDAP, AuthorizationType.Allow, null, null);
                            }
                            else
                            {
                                var       dbuser = application.GetDBUser(username);
                                IAzManSid sid    = dbuser.CustomSid;
                                role.CreateAuthorization(owner, whereDefined, sid, WhereDefined.Database, AuthorizationType.Allow, null, null);
                            }
                        }
                    }
                    storage.CommitTransaction();
                    //Rebuild StorageCache
                    this.InvalidateCache(false);
                }
                catch
                {
                    storage.RollBackTransaction();
                    throw;
                }
                finally
                {
                    storage.CloseConnection();
                }
            }
        }
 /// <summary>
 /// Removes the specified user names from the specified roles for the configured applicationName.
 /// </summary>
 /// <param name="usernames">A string array of user names to be removed from the specified roles.</param>
 /// <param name="roleNames">A string array of role names to remove the specified user names from.</param>
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (IAzManStorage storage = new SqlAzManStorage(this.storageCache.ConnectionString))
     {
         try
         {
             storage.OpenConnection();
             storage.BeginTransaction();
             IAzManApplication application = storage[this.storeName][this.applicationName];
             foreach (string roleName in roleNames)
             {
                 IAzManItem role = application.GetItem(roleName);
                 if (role.ItemType != ItemType.Role)
                 {
                     throw new ArgumentException(String.Format("{0} must be a Role.", roleName));
                 }
                 foreach (IAzManAuthorization auth in role.GetAuthorizations())
                 {
                     string displayName;
                     auth.GetMemberInfo(out displayName);
                     foreach (string username in usernames)
                     {
                         if (String.Compare(this.getFQUN(username), displayName, true) == 0)
                         {
                             auth.Delete();
                         }
                     }
                 }
             }
             storage.CommitTransaction();
             //Rebuild StorageCache
             this.InvalidateCache(false);
         }
         catch
         {
             storage.RollBackTransaction();
             throw;
         }
         finally
         {
             storage.CloseConnection();
         }
     }
 }
 /// <summary>
 /// Removes the specified user names from the specified roles for the configured applicationName.
 /// </summary>
 /// <param name="usernames">A string array of user names to be removed from the specified roles.</param>
 /// <param name="roleNames">A string array of role names to remove the specified user names from.</param>
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (IAzManStorage storage = new SqlAzManStorage(this.storageCache.ConnectionString))
     {
         try
         {
             storage.OpenConnection();
             storage.BeginTransaction();
             IAzManApplication application = storage[this.storeName][this.applicationName];
             foreach (string roleName in roleNames)
             {
                 IAzManItem role = application.GetItem(roleName);
                 if (role.ItemType != ItemType.Role)
                     throw new ArgumentException(String.Format("{0} must be a Role.", roleName));
                 foreach (IAzManAuthorization auth in role.GetAuthorizations())
                 {
                     string displayName;
                     auth.GetMemberInfo(out displayName);
                     foreach (string username in usernames)
                     {
                         if (String.Compare(this.getFQUN(username), displayName, true) == 0)
                         {
                             auth.Delete();
                         }
                     }
                 }
             }
             storage.CommitTransaction();
             //Rebuild StorageCache
             this.InvalidateCache(false);
         }
         catch
         {
             storage.RollBackTransaction();
             throw;
         }
         finally
         {
             storage.CloseConnection();
         }
     }
 }
        /// <summary>
        /// Adds the specified user names to the specified roles for the configured applicationName.
        /// </summary>
        /// <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>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            using (IAzManStorage storage = new SqlAzManStorage(this.storageCache.ConnectionString))
            {
                try
                {
                    storage.OpenConnection();
                    storage.BeginTransaction();
                    IAzManApplication application = storage[this.storeName][this.applicationName];
                    foreach (string roleName in roleNames)
                    {
                        IAzManItem role = application.GetItem(roleName);
                        if (role.ItemType != ItemType.Role)
                            throw new ArgumentException(String.Format("{0} must be a Role.", roleName));

                        foreach (string username in usernames)
                        {
                            IAzManSid owner = new SqlAzManSID(((System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity) ?? WindowsIdentity.GetCurrent()).User);
                            WhereDefined whereDefined = WhereDefined.LDAP;
                            if (this.userLookupType == "LDAP")
                            {
                                string fqun = this.getFQUN(username);
                                NTAccount ntaccount = new NTAccount(fqun);
                                if (ntaccount == null)
                                    throw SqlAzManException.UserNotFoundException(username, null);
                                IAzManSid sid = new SqlAzManSID(((SecurityIdentifier)(ntaccount.Translate(typeof(SecurityIdentifier)))));
                                if (sid == null)
                                    throw SqlAzManException.UserNotFoundException(username, null);
                                role.CreateAuthorization(owner, whereDefined, sid, WhereDefined.LDAP, AuthorizationType.Allow, null, null);
                            }
                            else
                            {
                                var dbuser = application.GetDBUser(username);
                                IAzManSid sid = dbuser.CustomSid;
                                role.CreateAuthorization(owner, whereDefined, sid, WhereDefined.Database, AuthorizationType.Allow, null, null);
                            }
                        }
                    }
                    storage.CommitTransaction();
                    //Rebuild StorageCache
                    this.InvalidateCache(false);
                }
                catch
                {
                    storage.RollBackTransaction();
                    throw;
                }
                finally
                {
                    storage.CloseConnection();
                }
            }
        }