Exemple #1
0
        public List <File> AddFiles(int fieldId, int docId, IEnumerable <File> files)
        {
            List <File> result = new List <File>();

            using (var db = new CMSContext())
            {
                foreach (File file in files)
                {
                    db.Files.Add(file);
                    db.SaveChanges();

                    var fileLink = new FileLink()
                    {
                        FieldId = fieldId,
                        DocId   = docId,
                        FileId  = file.Id
                    };

                    db.FileLinks.Add(fileLink);
                    db.SaveChanges();

                    result.Add(file);
                }
            }
            return(result);
        }
Exemple #2
0
        public Role CreateRoleWithSectionId(Role role, int sectionId)
        {
            using (var db = new CMSContext())
            {
                var newRole = db.Roles.Add(role);


                var views = db
                            .Sections
                            .Where(s => s.Id == sectionId)
                            .Include(s => s.Views)
                            .SelectMany(s => s.Views)
                            .Select(v => v);

                foreach (var view in views)
                {
                    var permission = new Permission()
                    {
                        PermissionType = PermissionType.Read,
                        Role           = newRole,
                        View           = view
                    };

                    db.Permissions.Add(permission);
                }

                db.SaveChanges();

                return(newRole);
            }
        }
Exemple #3
0
        public View CreateView(View view, string userLogin)
        {
            using (var db = new CMSContext())
            {
                var newView = db.Views.Add(view);

                if (newView.ParentView == null)
                {
                    var globalAdminsRole = db.Roles.FirstOrDefault(r => r.Name == Constants.ROLE_GLOBAL_ADMINS);

                    if (globalAdminsRole == null)
                    {
                        throw new CustomValidationException($"{Constants.ROLE_GLOBAL_ADMINS} role not found, can't create view.");
                    }

                    var currentUser = db.Users.FirstOrDefault(u => u.Login.ToUpper() == userLogin.ToUpper());

                    var viewAdminRole = db.Roles.Add(new Role()
                    {
                        Name        = $"{newView.Name} {Constants.ADMIN_POSTFIX}",
                        DisplayName = $"{RC.ADMINS_PREFIX} {newView.DisplayName}",
                    });

                    var userRole = new UserRole()
                    {
                        Role = viewAdminRole, User = currentUser, UserCanChangeRole = true
                    };

                    db.UserRoles.Add(userRole);

                    var roles = new List <Role> {
                        globalAdminsRole, viewAdminRole
                    };

                    foreach (var role in roles)
                    {
                        db.Permissions.Add(new Permission()
                        {
                            PermissionType = PermissionType.Write,
                            Role           = role,
                            View           = newView
                        });

                        db.Permissions.Add(new Permission()
                        {
                            PermissionType = PermissionType.ChangeSchema,
                            Role           = role,
                            View           = newView
                        });
                    }
                }

                db.SaveChanges();

                return(db.Views.Include(v => v.ChildViews)
                       .Include(v => v.Filters)
                       .Include(v => v.Controls)
                       .FirstOrDefault(v => v.Id == newView.Id));
            };
        }
Exemple #4
0
        public Field UpdateField(Field field)
        {
            using (var db = new CMSContext())
            {
                var dbField = db.Fields.Find(field.Id);

                if (dbField == null)
                {
                    throw new CustomValidationException($"No field with id = {field.Id}.");
                }
                if (field.Name.ToLower() != dbField.Name.ToLower())
                {
                    throw new CustomValidationException($"Not allowed to change field names, only display names.");
                }
                if (field.FieldType != dbField.FieldType)
                {
                    throw new CustomValidationException($"Not allowed to change field types.");
                }

                db.Entry(dbField).CurrentValues.SetValues(field);
                db.SaveChanges();
            };

            return(field);
        }
Exemple #5
0
 public void UpdateFile(File file)
 {
     using (var db = new CMSContext())
     {
         var fileEntity = db.Files.FirstOrDefault(f => f.Id == file.Id);
         db.Entry(fileEntity).CurrentValues.SetValues(file);
         db.SaveChanges();
     }
 }
Exemple #6
0
        public User CreateUser(User user)
        {
            using (var db = new CMSContext())
            {
                var newUser = db.Users.Add(user);
                db.SaveChanges();

                return(newUser);
            }
        }
Exemple #7
0
        public void DeleteAction(int id)
        {
            using (var db = new CMSContext())
            {
                var action = db.Actions.FirstOrDefault(a => a.Id == id);
                db.Actions.Remove(action);

                db.SaveChanges();
            }
        }
Exemple #8
0
        public void DeleteEvent(int id)
        {
            using (var db = new CMSContext())
            {
                var evt = db.Events.FirstOrDefault(e => e.Id == id);
                db.Events.Remove(evt);

                db.SaveChanges();
            }
        }
Exemple #9
0
        public Action CreateAction(Action action)
        {
            using (var db = new CMSContext())
            {
                var newAction = db.Actions.Add(action);
                db.SaveChanges();

                return(newAction);
            };
        }
Exemple #10
0
        public Section CreateSection(Section section)
        {
            using (var db = new CMSContext())
            {
                var newSection = db.Sections.Add(section);
                db.SaveChanges();

                return(db.Sections.Include(s => s.Views)
                       .FirstOrDefault(s => s.Id == newSection.Id));
            }
        }
Exemple #11
0
        public void DeleteStyle(int id)
        {
            using (var db = new CMSContext())
            {
                var style = db.Styles.FirstOrDefault(s => s.Id == id);

                db.Styles.Remove(style);

                db.SaveChanges();
            }
        }
Exemple #12
0
        public void DeleteParameters(IEnumerable <int> ids)
        {
            using (var db = new CMSContext())
            {
                var parameters = db.Parameters.Where(p => ids.Contains(p.Id));

                db.Parameters.RemoveRange(parameters);

                db.SaveChanges();
            };
        }
Exemple #13
0
        public void UpdatePermission(Permission permission)
        {
            using (var db = new CMSContext())
            {
                var dbPermission = db.Permissions.Find(permission.Id);

                db.Entry(dbPermission).CurrentValues.SetValues(permission);

                db.SaveChanges();
            }
        }
Exemple #14
0
        public void DeleteControlField(ControlField controlField)
        {
            using (var db = new CMSContext())
            {
                var dbControlField = db.ControlFields.FirstOrDefault(c => c.ControlId == controlField.ControlId &&
                                                                     c.FieldId == controlField.FieldId);

                db.ControlFields.Remove(dbControlField);
                db.SaveChanges();
            };
        }
Exemple #15
0
        public ControlField CreateControlField(ControlField controlField)
        {
            using (var db = new CMSContext())
            {
                var newControlField = db.ControlFields.Add(controlField);

                db.SaveChanges();

                return(db.ControlFields.FirstOrDefault(f => f.ControlId == newControlField.ControlId &&
                                                       f.FieldId == newControlField.FieldId));
            };
        }
Exemple #16
0
        public FilterField CreateFilterField(FilterField filterField)
        {
            using (var db = new CMSContext())
            {
                var newFilterField = db.FilterFields.Add(filterField);

                db.SaveChanges();

                return(db.FilterFields.FirstOrDefault(f => f.FilterId == newFilterField.FilterId &&
                                                      f.FieldId == newFilterField.FieldId && f.ChainId == newFilterField.ChainId));
            };
        }
Exemple #17
0
        public void DeleteFilterField(FilterField filterField)
        {
            using (var db = new CMSContext())
            {
                var dbFilterField = db.FilterFields.FirstOrDefault(c => c.FilterId == filterField.FilterId &&
                                                                   c.FieldId == filterField.FieldId && c.ChainId == filterField.ChainId);

                db.FilterFields.Remove(dbFilterField);

                db.SaveChanges();
            };
        }
Exemple #18
0
        public Style CreateStyle(Style style)
        {
            using (var db = new CMSContext())
            {
                var newStyle = db.Styles.Add(style);


                db.SaveChanges();

                return(newStyle);
            }
        }
Exemple #19
0
        public Event CreateEvent(Event evt)
        {
            using (var db = new CMSContext())
            {
                evt.Control = db.Controls.Single(c => c.Id == evt.ControlId);

                var newEvent = db.Events.Add(evt);
                db.SaveChanges();

                return(newEvent);
            }
        }
Exemple #20
0
        public Control CreateControl(Control control)
        {
            using (var db = new CMSContext())
            {
                var view = db.Views.FirstOrDefault(v => v.Id == control.ViewId);
                control.View = view ?? throw new CustomValidationException($"View with id = {control.ViewId} does not exist.");
                var newControl = db.Controls.Add(control);

                db.SaveChanges();

                return(db.Controls.FirstOrDefault(c => c.Id == newControl.Id));
            };
        }
Exemple #21
0
        public Action UpdateAction(Action action)
        {
            using (var db = new CMSContext())
            {
                var dbAction = db.Actions
                               .Include(f => f.Parameters)
                               .FirstOrDefault(f => f.Id == action.Id);

                var pIds           = action.Parameters.Select(p => p.Id).ToList();
                var existingParams = db.Parameters.Where(f => pIds.Contains(f.Id));

                var existingParamIds = existingParams.Select(p => p.Id);

                foreach (var existParam in existingParams)
                {
                    db.Entry(existParam).CurrentValues.SetValues(action.Parameters.First(p => p.Id == existParam.Id));
                }

                var newParams = action.Parameters.Where(p => !existingParamIds.Contains(p.Id)).ToList();

                db.Parameters.AddRange(newParams);
                db.SaveChanges();

                var parameters = db.Parameters.Where(p => p.ActionId == action.Id).ToList();

                action.Parameters.Clear();

                foreach (var p in parameters)
                {
                    dbAction.Parameters.Add(p);
                }


                db.Entry(dbAction).CurrentValues.SetValues(action);
                db.SaveChanges();

                return(dbAction);
            };
        }
Exemple #22
0
        public void DeleteSection(int id)
        {
            using (var db = new CMSContext())
            {
                var section = db.Sections.FirstOrDefault(s => s.Id == id);
                if (section == null)
                {
                    throw new CustomValidationException($"Section with id = {id} does not exist.");
                }

                db.Sections.Remove(section);
                db.SaveChanges();
            }
        }
Exemple #23
0
        public void DeleteControl(int id)
        {
            using (var db = new CMSContext())
            {
                var control = db.Controls.FirstOrDefault(v => v.Id == id);
                if (control == null)
                {
                    throw new CustomValidationException($"Control with id = {id} does not exist.");
                }

                db.Controls.Remove(control);
                db.SaveChanges();
            };
        }
Exemple #24
0
        public void DeleteFilter(int id)
        {
            using (var db = new CMSContext())
            {
                var filter = db.Filters.FirstOrDefault(f => f.Id == id);
                if (filter == null)
                {
                    throw new CustomValidationException($"Filter with id = {id} does not exist.");
                }

                db.Filters.Remove(filter);
                db.SaveChanges();
            };
        }
Exemple #25
0
        public Filter CreateFilter(Filter filter)
        {
            using (var db = new CMSContext())
            {
                var view = db.Views.FirstOrDefault(v => v.Id == filter.ViewId);
                filter.View = view;

                var newFilter = db.Filters.Add(filter);

                db.SaveChanges();

                return(db.Filters.FirstOrDefault(f => f.Id == newFilter.Id));
            };
        }
Exemple #26
0
        public void RemoveUserFromRole(UserRole userRole, string userLogin)
        {
            using (var db = new CMSContext())
            {
                var currentUser = db.Users.FirstOrDefault(u => u.Login.ToUpper() == userLogin.ToUpper());

                if (!db.UserRoles.Any(ur => ur.RoleId == userRole.RoleId && ur.UserId == currentUser.Id && ur.UserCanChangeRole))
                {
                    throw new CustomValidationException("Current user doesn't have rights to delete permissions from this role.");
                }

                var dbUserRole = db.UserRoles.FirstOrDefault(ur => ur.RoleId == userRole.RoleId && ur.UserId == userRole.UserId);

                db.UserRoles.Remove(dbUserRole);
                db.SaveChanges();
            }
        }
Exemple #27
0
        public UserRole AddUserToRole(UserRole userRole, string userLogin)
        {
            using (var db = new CMSContext())
            {
                var currentUser = db.Users.FirstOrDefault(u => u.Login.ToUpper() == userLogin.ToUpper());

                if (!db.UserRoles.Any(ur => ur.RoleId == userRole.RoleId && ur.UserId == currentUser.Id && ur.UserCanChangeRole))
                {
                    throw new CustomValidationException("Current user doesn't have rights to add users to the role.");
                }

                var newUserRole = db.UserRoles.Add(userRole);
                db.SaveChanges();

                return(newUserRole);
            }
        }
Exemple #28
0
        public View UpdateView(View view)
        {
            using (var db = new CMSContext())
            {
                var dbView = db.Views.Find(view.Id);

                if (dbView == null)
                {
                    throw new CustomValidationException($"No view with id = {view.Id}.");
                }

                db.Entry(dbView).CurrentValues.SetValues(view);
                db.SaveChanges();
            };

            return(view);
        }
Exemple #29
0
        public Style UpdateStyle(Style style)
        {
            using (var db = new CMSContext())
            {
                var dbStyle = db.Styles.Find(style.Id);

                if (dbStyle == null)
                {
                    throw new CustomValidationException($"No style with id = {style.Id}.");
                }

                db.Entry(dbStyle).CurrentValues.SetValues(style);
                db.SaveChanges();
            };

            return(style);
        }
Exemple #30
0
        public Control UpdateControl(Control control)
        {
            using (var db = new CMSContext())
            {
                var dbControl = db.Controls.Find(control.Id);

                if (dbControl == null)
                {
                    throw new CustomValidationException($"No control with id = {control.Id}.");
                }

                db.Entry(dbControl).CurrentValues.SetValues(control);
                db.SaveChanges();
            };

            return(control);
        }