BExISMigration.SecurityMigration.GetBexisRights C# (CSharp) Method

GetBexisRights() private method

private GetBexisRights ( string dataBase, int>.Dictionary dataSetsMapping ) : List
dataBase string
dataSetsMapping int>.Dictionary
return List
        internal List<Right> GetBexisRights(string dataBase,  Dictionary<int, int> dataSetsMapping)
        {
            List<Right> bexisRights = new List<Right>();
            string datasetQuery = "";
            foreach (var dataSetMapping in dataSetsMapping)
            {
                datasetQuery += "DATASETID = "+ dataSetMapping.Key;
                if (dataSetsMapping.Last().Key != dataSetMapping.Key)
                    datasetQuery += " or ";
            }
            if (dataSetsMapping.Any())
            {
                datasetQuery = "where " + datasetQuery + "";
            }
            // DB query
            string mySelectQuery = "SELECT ROLENAME, DATASETID, FOREDIT, APPLICATIONNAME FROM \"PROVIDER\".\"RIGHTS\"   "+ datasetQuery;
            DB2Connection connect = new DB2Connection(dataBase);
            DB2Command myCommand = new DB2Command(mySelectQuery, connect);
            connect.Open();
            DB2DataReader myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                bexisRights.Add(new Right()
                {
                    RoleName = myReader.GetString(0),
                    DataSetId = (int)(myReader.GetValue(1)),
                    CanEdit = myReader.GetString(2)=="N"?false:true
                });
            }
            myReader.Close();
            connect.Close();
            return bexisRights;
        }

Usage Example

Example #1
0
        public string TransferDataPermission()
        {
            SubjectManager subjectManager = new SubjectManager();
            var securityMigration = new SecurityMigration();
            Dictionary<int, int> DataSetIDs = new Dictionary<int, int>();
            var groups = subjectManager.GetAllGroups();
            string DatasetMappingPath = Path.Combine(AppConfiguration.DataPath, "DatasetMapping.txt");
            //Key is last datasetId and value is the new one
            Dictionary<int, int> DatasetsMapping = File.ReadAllLines(DatasetMappingPath).AsEnumerable().Select(item => new { oldId = int.Parse(item.Split('\t')[0]), newId = int.Parse(item.Split('\t')[1]) }).ToDictionary(c => c.oldId, c => c.newId);
            DatasetManager dm = new DatasetManager();

            PermissionManager permissionManager = new PermissionManager();
            List<SecurityMigration.Right> rights = securityMigration.GetBexisRights(DataBase, DatasetsMapping);
            foreach (var group in groups)
            {
                var groupRights = rights.Where(item => item.RoleName == group.Name || item.RoleName == "_" + group.Name);

                foreach (var right in groupRights)
                {
                    int newDataSetId = DatasetsMapping.FirstOrDefault(item => item.Key == right.DataSetId).Value;

                    //each entity wich exists in this list has view and download feature
                    permissionManager.CreateDataPermission(group.Id, 1, newDataSetId, RightType.View);
                    permissionManager.CreateDataPermission(group.Id, 1, newDataSetId, RightType.Download);

                    if (right.CanEdit)
                        permissionManager.CreateDataPermission(group.Id, 1, newDataSetId, RightType.Update);

                }
            }
            foreach (var DatasetMapping in DatasetsMapping)
            {
                //extract grant user from the last version and add it to new ver
                if (dm.GetDataset(DatasetMapping.Value) == null)
                    continue;
                DatasetVersion dsv = dm.GetDatasetLatestVersion(DatasetMapping.Value);
                string grantUserEmailAddress = dsv.Metadata.SelectSingleNode("Metadata/general/general/designatedDatasetManager/contactType/email/email").InnerText;
                if (!string.IsNullOrEmpty(grantUserEmailAddress))
                {
                    var grantUser = subjectManager.GetUserByEmail(grantUserEmailAddress);
                    permissionManager.CreateDataPermission(grantUser.Id, 1, DatasetMapping.Value, RightType.Grant);
                }
            }
            return "All of permissions transfered successfully.";
        }