Esempio n. 1
0
        public static Boolean AssignRights(UserSession Session, String CardID)
        {
            logger.Info("cardId='{0}'", CardID);
            using (new Impersonator(ServerExtension.Domain, ServerExtension.User, ServerExtension.SecurePassword))
            {
                Guid CardId = new Guid(CardID);
                switch (Session.CardManager.GetCardState(CardId))
                {
                case ObjectState.Existing:
                    CardData Card = Session.CardManager.GetCardData(CardId);

                    List <TargetObject> TObjects = AssignHelper.LoadMatrix(Settings.MatrixPath, Settings.CardSheetName, Settings.Domain);
                    Card.UnlockCard();
                    logger.Debug("Card unlocked.'");
                    ExtraCard Extra = Card.GetExtraCard();
                    if (Extra.IsNull())
                    {
                        Extra = new ExtraCard(Card);
                    }
                    logger.Debug("ExtraCard received.'");

                    if (!Extra.IsNull())
                    {
                        String CategoryName = Extra.CategoryName;
                        logger.Info("Категория = '{0}'", CategoryName);

                        if (!String.IsNullOrEmpty(CategoryName))
                        {
                            TargetObject TObject = TObjects.FirstOrDefault(to => to.Name == CategoryName);
                            if (!TObject.IsNull())
                            {
                                CardDataSecurity Rights = Card.GetAccessControl();
                                Rights.SetOwner(new NTAccount(Settings.Domain, "DV Admins"));
                                Rights = Rights.RemoveExplicitRights();


                                foreach (AssignGroup Group in TObject.Groups)
                                {
                                    try { Rights.SetAssignGroup(TObject, Group); }
                                    catch (Exception ex) { logger.ErrorException(String.Format("Группа '{0}'", Group.Name), ex); }
                                }

                                AssignGroup[] FolderGroups = TObject.Groups.Where(g => g.DirectoryRights != 0).ToArray();
                                if (FolderGroups.Length > 0)
                                {
                                    String FolderPath = Extra.Path;
                                    if (!String.IsNullOrEmpty(FolderPath))
                                    {
                                        DirectoryInfo Folder = new DirectoryInfo(FolderPath);
                                        if (Folder.Exists)
                                        {
                                            foreach (AssignGroup Group in FolderGroups)
                                            {
                                                try { Folder.SetAssignGroup(TObject, Group); }
                                                catch (Exception ex) { logger.ErrorException(String.Format("Группа '{0}'", Group.Name), ex); }
                                            }
                                        }

                                        else
                                        {
                                            logger.Warn(String.Format("Карточка '{0}' ссылается на несуществующую папку'{1}'", Card.Id, Folder.FullName));
                                        }
                                    }
                                }

                                Card.SetAccessControl(Rights);
                                logger.Info("Права назначены!");
                                return(true);
                            }
                            logger.Warn("Категория '{0}' не найдена в матрице.", CategoryName);
                        }
                    }

                    logger.Info("Права не назначены.");
                    return(false);

                default:
                    return(false);
                }
            }
        }