Application Group methods?

Topics: General Topic, NetSqlAzMan Core
Mar 25, 2011 at 11:59 AM
Edited Mar 25, 2011 at 3:02 PM

Hello Andrea,

what are the methods on v3.6.0.5 for Add/Remove users to Application Groups ?

Thank you, regards

Claudio

Mar 25, 2011 at 4:15 PM

Solved.

        /// <summary>
        /// Add user on Application Group.
        /// If user belong already other application group, these will be removed from that.
        /// If application group is not exist, will be create.
        /// </summary>
        public static void AddUserToApplicationGroup(string userName, string appGroupName)
        {
            try
            {

                IAzManApplicationGroup[] appGroups = _app.GetApplicationGroups();

                // verifica se esiste il gruppo
                bool fGroupExist = false;
                foreach (IAzManApplicationGroup group in appGroups)
                {
                    if (appGroupName.ToUpper() == group.Name.ToUpper())
                    {
                        fGroupExist = true;
                        break;
                    }
                }

                // se non esiste lo crea
                if (!fGroupExist)
                    _app.CreateApplicationGroup(SqlAzManSID.NewSqlAzManSid(), appGroupName, String.Empty, String.Empty, GroupType.Basic);

                IAzManApplicationGroup appGroup = _app.GetApplicationGroup(appGroupName);

                IAzManDBUser dbUser = _cacheStorage.Storage.GetDBUser(userName);

                // verifica se l'utente era gia' associato ad altro gruppo
                foreach (IAzManApplicationGroup group in appGroups)
                {
                    if (group.IsInGroup(dbUser))
                    {
                        // se era gia' membro di altro gruppo, lo rimuovo da questo
                        IAzManApplicationGroupMember member = group.GetApplicationGroupMember(dbUser.CustomSid);
                        member.Delete();
                        break;
                    }
                }

                // infine, inserisco l'utente nel gruppo indicato
                appGroup.CreateApplicationGroupMember(dbUser.CustomSid, WhereDefined.Database, true);

            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.Message, ex);
                throw;
            }
        }