Exemplo n.º 1
0
        private void btn_rmUser_Save_Click(object sender, EventArgs e)
        {
            Program.ExitCode status;

            if (MessageBox.Show(this,
                            "Удалить пользователя?",
                            "Warning",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Warning) == DialogResult.No)
            {
                return;
            }

            //удалить пользователя после подтверждения:
            User u = new User
            {
                Id = Convert.ToInt32(cb_Name.SelectedValue),
                Name = cb_Name.Text,
                Password = tb_Password.Text,
                Policy_Id = Convert.ToInt32(cb_Policy.SelectedValue)
            };

            status = RBACManager.RmUser(u, mainForm.db);

            if (status == Program.ExitCode.Success)
            {
                MessageBox.Show(this,
                            "Пользователь удален!",
                            "Success",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                this.Close();
                return;
            }
            if (status == Program.ExitCode.Error)
            {
                MessageBox.Show(this,
                            "Error while submitting deletion to the DataBase",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                return;
            }
        }
Exemplo n.º 2
0
        private void btn_drawURP_Save_Click(object sender, EventArgs e)
        {
            Program.ExitCode status;

            //Получить пользователя:
            User u = new User
            {
                Id = Convert.ToInt32(cb_Name.SelectedValue),
                Name = cb_Name.Text,
                Password = tb_Password.Text,
                Policy_Id = Convert.ToInt32(cb_Policy.SelectedValue)
            };

            // Отрисовка диаграммы:
            if (mainForm.axDrawingControl.Document.Application.ActivePage.Shapes.Count == 0)
                status = Visualizer.VisualizeURP(mainForm.axDrawingControl.Document.Application.ActivePage,
                                                mainForm.db,
                                                u);
            else
                status = Visualizer.VisualizeURP(mainForm.axDrawingControl.Document.Pages.Add(),
                                                mainForm.db,
                                                u);

            if (status == Program.ExitCode.Error)
            {
                MessageBox.Show(this,
                            "Error while drawing URP relation!",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                return;
            }

            this.Close();
            return;
        }
Exemplo n.º 3
0
        private void btn_addUser_Save_Click(object sender, EventArgs e)
        {
            Program.ExitCode status;

            if (tb_Name.Text != "" && tb_Pwd.Text != "")
            {
                if (cb_Policy.Text != "")
                {
                    User u = new User
                    {
                        Name = tb_Name.Text,
                        Password = tb_Pwd.Text,
                        Policy_Id = Convert.ToInt32(cb_Policy.SelectedValue)
                    };

                    status = RBACManager.AddUser(u, mainForm.db);
                    if (status == Program.ExitCode.Error)
                    {
                        MessageBox.Show(this,
                                            "Error while Submiting results in the DataBase!",
                                            "Error",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                        return;
                    }
                    if (status == Program.ExitCode.Success)
                    {
                        MessageBox.Show(this,
                                        "Пользователь добавлен!",
                                        "Success",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                        this.Close();
                        return;
                    }
                    if (status == Program.ExitCode.ElementExists)
                    {
                        if (MessageBox.Show(this,
                                    "Пользователь с такими 'Name' и 'Policy_Id' уже существует!\nОбновить данные для этого пользователя?",
                                    "Warning",
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            status = RBACManager.UpdateUser(u, mainForm.db);
                            if (status == Program.ExitCode.Error)
                            {
                                MessageBox.Show(this,
                                            "Error while Submiting results in the DataBase!",
                                            "Error",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                                return;
                            }
                            if (status == Program.ExitCode.Success)
                            {
                                MessageBox.Show(this,
                                            "Данные пользователя обновлены!",
                                            "Success",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Information);
                                this.Close();
                                return;
                            }
                        }
                        //MsgBox."NO" pressed => Ignore changes, return to add-Form
                        else
                        {
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show(this,
                                "Выберите политику 'Policy'!",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show(this,
                            "Вы не заполнили одно из полей 'Name' или 'Password'!",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }
Exemplo n.º 4
0
 partial void DeleteUser(User instance);
Exemplo n.º 5
0
 partial void UpdateUser(User instance);
Exemplo n.º 6
0
 partial void InsertUser(User instance);
Exemplo n.º 7
0
		private void detach_User(User entity)
		{
			this.SendPropertyChanging();
			entity.Policy = null;
		}
Exemplo n.º 8
0
		private void attach_User(User entity)
		{
			this.SendPropertyChanging();
			entity.Policy = this;
		}
Exemplo n.º 9
0
        public static Program.ExitCode VisualizeURP(Visio.Page targetPage, rbacLINQ2SQLDataContext db, User user_in)
        {
            Visio.Application visioApplication = null;
            Visio.Document stencilUMLUseCase = null;
            Visio.Document stencilBasicU = null;
            Visio.Shape shape = null;
            Visio.Shape shapeContainer = null;
            Visio.Shape shapeConnector = null;
            List<Visio.Shape> shapeRoles = null;
            List<Visio.Shape> shapePermissions = null;
            Visio.Selection selection = null;
            Visio.Document targetDocument = null;
            Visio.ContainerProperties containerProperties = null;
            int currentDiagramServices = -1;
            int prevDiagramServices;
            System.Array containerMembers = null;

            if (user_in == null || targetPage == null)
                return Program.ExitCode.Error;
            var users = from usr in db.User
                        where usr.Name == user_in.Name && usr.Policy_Id == user_in.Policy_Id
                        select usr;
            //if doesn't exist, return Error status:
            if (users.Count() == 0)
                return Program.ExitCode.ElementDoesNotExists;

            try
            {
                // Turn on all Visio diagram services
                targetDocument = targetPage.Document;
                visioApplication = targetPage.Application;

                // Enable All Diagram Services to enable adding huge-named members to the container
                prevDiagramServices = targetDocument.DiagramServicesEnabled;
                targetDocument.DiagramServicesEnabled = currentDiagramServices;

                stencilUMLUseCase = visioApplication.Documents.
                                        OpenEx(@"C:\MyTestProjects\MCD\diploma\pmtool\pmt\pmt\UML_Use_Case.vssx",
                                        (short)Visio.VisOpenSaveArgs.visOpenHidden);
                stencilBasicU = visioApplication.Documents.
                                        OpenEx(@"Basic_U.vssx",
                                        (short)Visio.VisOpenSaveArgs.visOpenHidden);

                selection = targetPage.CreateSelection(Visio.VisSelectionTypes.visSelTypeEmpty,
                                                       Visio.VisSelectMode.visSelModeOnlySuper, null);
                //=======================================

                // Get all necessary data for drawing a diagram
                User u = users.First();
                var roles = from auth in u.AuthUserRole
                            select auth.Role;
                Dictionary<Role, List<Permission>> rpSet = new Dictionary<Role, List<Permission>>();
                shapePermissions = new List<Visio.Shape>();

                foreach (Role r in roles)
                {
                    var perms = from roleperm in r.RolePermission
                                select roleperm.Permission;
                    rpSet.Add(r, perms.ToList<Permission>());
                }

                double H = 0.5;
                double W = 4;
                double gap = 0.5;
                double centerX = 5;
                double centerY = 1;
                double border = 0.1;

                // Draw permissions with general method DropConnected
                List<string> strPerms = new List<string>();
                int maxStrLenPerms = 0;
                foreach (var rp in rpSet)
                {
                    if (rp.Value.Count == 0)
                    {
                        strPerms.Add("NO PERMISSION ASSIGNED");
                        maxStrLenPerms = strPerms.Last().Length;
                    }
                    else
                    {
                        foreach (Permission p in rp.Value)
                        {
                            if (maxStrLenPerms == 0)
                                strPerms.Add(String.Format("Name: {0}, Policy_Id: {1}", p.Name, p.Policy_Id));
                            else
                                strPerms.Add(String.Format("\nName: {0}, Policy_Id: {1}", p.Name, p.Policy_Id));

                            if (strPerms.Last().Length > maxStrLenPerms)
                                maxStrLenPerms = strPerms.Last().Length;
                        }
                    }
                    shape = targetPage.Drop(stencilBasicU.Masters["Rectangle"],centerX, centerY += H + gap);
                    double charSize = shape.get_Cells("Char.Size").ResultIU;
                    shape.get_Cells("Height").ResultIU = charSize * strPerms.Count + 2 * border;
                    shape.get_Cells("Width").ResultIU = charSize * maxStrLenPerms;
                    shape.Text = String.Concat(strPerms);

                    shapePermissions.Add(shape);

                    strPerms.Clear();
                    maxStrLenPerms = 0;
                }

                //Left-Side Alignment of Permission Rectangles
                if (shapePermissions.Count != 0)
                {
                    foreach (var sh in shapePermissions)
                        selection.Select(sh, (short)Visio.VisSelectArgs.visSelect);
                    selection.Align(Visio.VisHorizontalAlignTypes.visHorzAlignLeft,
                                    Visio.VisVerticalAlignTypes.visVertAlignNone);
                    selection.DeselectAll();
                }

                // Drop Roles (Use Case Objects)
                shapeRoles = new List<Visio.Shape>();
                int i = 0;
                foreach (var rp in rpSet)
                {
                    shape = targetPage.DropConnected(stencilUMLUseCase.Masters["Use Case"],
                                                    shapePermissions.ElementAt(i),
                                                    Visio.VisAutoConnectDir.visAutoConnectDirLeft);
                    shape.Text = String.Format("name: {0}|policy: {1}|cardinality:{2}",
                                                rp.Key.Name, rp.Key.Policy_Id, rp.Key.Cardinality);
                    shapeRoles.Add(shape);
                    selection.Select(shape, (short)Visio.VisSelectArgs.visSelect);
                    i++;
                }

                // Drop a container
                shapeContainer = targetPage.DropContainer(stencilUMLUseCase.Masters["Subsystem"],
                                                            (shapeRoles.Count==0?null:selection));
                shapeContainer.Text = (shapeRoles.Count==0?"NO ROLE AUTHORIZED":"Authorized roles");
                selection.DeselectAll();

                // Move Container to the left to avoid overlapping with Permission Rectangles
                selection.Select(shapeContainer, (short)Visio.VisSelectArgs.visSelect);
                selection.Move(-1, 0);
                selection.DeselectAll();

                // Get height of the container and add a User to the left.
                // Can't use general method DropConnected, cause the container doesn't have such a property
                double containerH = shapeContainer.get_Cells("Height").ResultIU;
                double containerW = shapeContainer.get_Cells("Width").ResultIU;
                double containerXPos = shapeContainer.get_Cells("PinX").ResultIU;
                double containerYPos = shapeContainer.get_Cells("PinY").ResultIU;
                shape = targetPage.Drop(stencilUMLUseCase.Masters["Actor"], containerXPos - containerW / 2 - 1, containerYPos);
                shape.Text = String.Format("USER\nName: {0}\nPassword: {1}\nPolicy_Id: {2}",
                                            user_in.Name,
                                            user_in.Password,
                                            user_in.Policy_Id);
                shapeConnector = targetPage.Drop(stencilUMLUseCase.Masters["Association"], 0, 0);
                ConnectShapes(shape, shapeContainer, shapeConnector);

                // Report on contents of container
                containerProperties = shapeContainer.ContainerProperties;
                containerMembers = containerProperties.GetMemberShapes((int)Visio.VisContainerFlags.visContainerFlagsDefault);
                foreach (int member in containerMembers)
                {
                    System.Diagnostics.Debug.WriteLine(targetPage.Shapes.get_ItemFromID(member).NameU +
                        " |---> " + targetPage.Shapes.get_ItemFromID(member).Text);
                }

                // Finalize
                targetPage.Application.ActiveWindow.DeselectAll();
                targetPage.CenterDrawing();
                stencilUMLUseCase.Close();
                //targetPage.Name = String.Format("URP | Usr:{0};Pol:{1}",user_in.Name,user_in.Policy_Id);
                targetDocument.DiagramServicesEnabled = prevDiagramServices;
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                // Return the Diagram Services status to its previous state if it was set in the try block.
                if (currentDiagramServices != -1 && targetDocument != null)
                    targetDocument.DiagramServicesEnabled = currentDiagramServices;
                throw;
                //return Program.ExitCode.Error;
            }

            return Program.ExitCode.Success;
        }
Exemplo n.º 10
0
        public static Program.ExitCode RmUser(User u, rbacLINQ2SQLDataContext db, bool submitChanges = true)
        {
            try
            {
                User user = db.User.Single(u1 => (u1.Id == u.Id && u1.Name == u.Name &&
                                                  u1.Policy_Id == u.Policy_Id));

                //Можем запрещать удалять Юзера, у которого есть assigned roles.
                //if (user.AuthUserRole.Count != 0)
                //{
                //    return Program.ExitCode.HasAssigned;
                //}
                foreach (Session s in user.Session)
                {
                    db.ActiveRole.DeleteAllOnSubmit(s.ActiveRole);
                }
                db.Session.DeleteAllOnSubmit(user.Session);
                db.AuthUserRole.DeleteAllOnSubmit(user.AuthUserRole);
                db.User.DeleteOnSubmit(db.User.Single(u1 =>
                                                (u1.Id == u.Id &&
                                                u1.Policy_Id == u.Policy_Id)));
                if (submitChanges)
                    db.SubmitChanges();

                return Program.ExitCode.Success;
            }
            catch (Exception exc)
            {
                return Program.ExitCode.Error;
            }
        }
Exemplo n.º 11
0
 public static Program.ExitCode UpdateUser(User u, rbacLINQ2SQLDataContext db)
 {
     var query = from usr in db.User
                 where usr.Name == u.Name && usr.Policy_Id == u.Policy_Id
                 select usr;
     query.First().Password = u.Password;
     try
     {
         db.SubmitChanges();
         return Program.ExitCode.Success;
     }
     catch (Exception exc)
     {
         return Program.ExitCode.Error;
     }
 }
Exemplo n.º 12
0
 public static void AddUser_noTryCatch(User u, rbacLINQ2SQLDataContext db)
 {
     //check if the user exists
     var query = from usr in db.User
                 where usr.Name == u.Name && usr.Policy_Id == u.Policy_Id
                 select usr;
     //if doesn't exist, add:
     if (query.Count() == 0)
     {
         db.User.InsertOnSubmit(u);
         db.SubmitChanges();
         System.Diagnostics.Debug.WriteLine("Added User: {0}", u.Name);
     }
     else
     {
         System.Diagnostics.Debug.WriteLine("Existing User: {0}", u.Name);
     }
 }
Exemplo n.º 13
0
 public static Program.ExitCode AddUser(User u, rbacLINQ2SQLDataContext db)
 {
     //check if the user exists
     var query = from usr in db.User
                 where usr.Name == u.Name && usr.Policy_Id == u.Policy_Id
                 select usr;
     //if doesn't exist, add:
     if (query.Count() == 0)
     {
         db.User.InsertOnSubmit(u);
         try
         {
             db.SubmitChanges();
             return Program.ExitCode.Success;
         }
         catch (Exception exc)
         {
             return Program.ExitCode.Error;
         }
     }
     //if exists:
     else
     {
         return Program.ExitCode.ElementExists;
     }
 }
Exemplo n.º 14
0
 private static void XMLWriteUserAuthRoles(User user, XmlWriter xmlWriter)
 {
     xmlWriter.WriteStartElement("AuthRoles");
     foreach (AuthUserRole authUR in user.AuthUserRole)
     {
         xmlWriter.WriteStartElement("Role");
         xmlWriter.WriteAttributeString("name", authUR.Role.Name);
         xmlWriter.WriteEndElement();
     }
     xmlWriter.WriteEndElement();
 }
Exemplo n.º 15
0
 private static void XMLWriteUser(User user, XmlWriter xmlWriter)
 {
     xmlWriter.WriteStartElement("User");
         xmlWriter.WriteAttributeString("name", user.Name);
         xmlWriter.WriteAttributeString("password", user.Password);
         XMLWriteUserAuthRoles(user, xmlWriter);
     xmlWriter.WriteEndElement();
 }
Exemplo n.º 16
0
        private static bool XMLReadUser(XmlNode userNode, int pid, rbacLINQ2SQLDataContext db)
        {
            User user;
            Role role;
            AuthUserRole authUR;

            user = new User()
            {
                Name = userNode.Attributes["name"].Value,
                Password = userNode.Attributes["password"].Value,
                Policy_Id = pid,
            };
            RBACManager.AddUser_noTryCatch(user, db);
            // Depending on our specification we could have to update an existing user
            //RBACManager.UpdateUser(user,db);

            // Get this user (just added to the database or existing in it)
            user = db.User.Single(x => x.Name == user.Name && x.Policy_Id == user.Policy_Id);

            // If there's no single AuthRoles block, error:
            if (userNode.ChildNodes.Count != 1)
            {
                return false;
            }
            XmlNode authRoleBlock = userNode.ChildNodes.Item(0);
            foreach (XmlNode authRoleNode in authRoleBlock.ChildNodes)
            {
                role = db.Role.Single(x => x.Name == authRoleNode.Attributes["name"].Value
                                                    && x.Policy_Id == pid);
                authUR = new AuthUserRole()
                {
                    User_Id = user.Id,
                    Role_Id = role.Id,
                };
                RBACManager.AddAssignment_noTryCatch(authUR, db);
            }

            return true;
        }