public UserRoleStatus AddUserRole(UserRoleDTO role)
        {
            if (string.IsNullOrWhiteSpace(role.Name))
            {
                return(UserRoleStatus.ElementEmpty);
            }

            if (role.Permissions.Count == 0)
            {
                return(UserRoleStatus.NoPermissionsSelected);
            }

            var rolesManagingPermissions = new[] { 7, 8, 9 };

            if (role.Permissions.Any(p => rolesManagingPermissions.Contains(p.Id)) && !role.Permissions.Any(pe => pe.Id == 6))
            {
                return(UserRoleStatus.NoRolesBrowsingSelected);
            }

            var usersManagingPermissions = new[] { 2, 3, 4, 5 };

            if (role.Permissions.Any(p => usersManagingPermissions.Contains(p.Id)) && !role.Permissions.Any(pe => pe.Id == 1))
            {
                return(UserRoleStatus.NoUsersBrowsingSelected);
            }

            var tasksManagingPermissions = new[] { 11, 12, 13 };

            if (role.Permissions.Any(p => tasksManagingPermissions.Contains(p.Id)) && !role.Permissions.Any(pe => pe.Id == 10))
            {
                return(UserRoleStatus.NoTasksBrowsingSelected);
            }

            using (var session = Hibernate.SessionFactory.OpenSession())
            {
                if (session.QueryOver <UserRole>().Where(r => r.Name == role.Name).RowCount() > 0)
                {
                    return(UserRoleStatus.RolenameExists);
                }

                var newRole = new UserRole
                {
                    IsSystemRole = false,
                    Name         = role.Name,
                    Permissions  = session.QueryOver <Permission>().Where(Res.In("Id", role.Permissions.Select(p => p.Id).ToList())).List()
                };

                session.Save(newRole);
                session.Flush();

                return(UserRoleStatus.Added);
            }
        }
        public static ICriterion ProcessIsInCollection(MethodCallExpression methodCallExpression)
        {
            ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
            ICollection values = (ICollection)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(projection.Create <ICriterion>(s => Restrictions.In(s, values), p => Restrictions.In(p, values)));
        }
예제 #3
0
        public TaskCreationStatus AddModifyTask(TaskCreationDTO task)
        {
            if (task.EndPeriod.HasValue && task.EndPeriod.Value < task.StartPeriod)
            {
                return(TaskCreationStatus.EndPeriodEarlierThanStartPeriod);
            }

            if (!task.GenreId.HasValue)
            {
                return(TaskCreationStatus.GenreNotSelected);
            }
            if (!task.StatusId.HasValue)
            {
                return(TaskCreationStatus.StatusNotSelected);
            }
            if (!task.PriorityId.HasValue)
            {
                return(TaskCreationStatus.PriorityNotSelected);
            }

            if (!task.ParticipantsIds.Any())
            {
                return(TaskCreationStatus.PatricipantsNotSelected);
            }

            var textFields = new List <string> {
                task.Topic, task.Content
            };

            if (textFields.Any(t => string.IsNullOrWhiteSpace(t)))
            {
                return(TaskCreationStatus.ElementEmpty);
            }

            using (var session = Hibernate.SessionFactory.OpenSession())
            {
                var closedTaskStatusId = session.QueryOver <TaskStatus>()
                                         .Where(s => s.Name == Constants.TaskStatus_Closed)
                                         .Select(s => s.Id)
                                         .SingleOrDefault <int>();

                if (task.StatusId.HasValue && task.StatusId.Value == closedTaskStatusId && !task.EndPeriod.HasValue)
                {
                    return(TaskCreationStatus.ClosedOrCanceledWithNoEndPeriod);
                }

                using (var transaction = session.BeginTransaction())
                {
                    Task taskEntity = task.Id.HasValue
                        ? session.QueryOver <Task>().Where(t => t.Id == task.Id.Value).SingleOrDefault()
                        : new Task();

                    taskEntity.Topic       = task.Topic;
                    taskEntity.Content     = task.Content;
                    taskEntity.Author      = task.Author;
                    taskEntity.StartPeriod = task.StartPeriod;
                    taskEntity.EndPeriod   = task.EndPeriod;
                    taskEntity.Status      = session.QueryOver <TaskStatus>().Where(s => s.Id == task.StatusId.Value).SingleOrDefault();
                    taskEntity.Genre       = session.QueryOver <TaskGenre>().Where(g => g.Id == task.GenreId.Value).SingleOrDefault();
                    taskEntity.Priority    = session.QueryOver <TaskPriority>().Where(p => p.Id == task.PriorityId.Value).SingleOrDefault();
                    taskEntity.Users       = session.QueryOver <User>().Where(Res.In("Id", task.ParticipantsIds.ToArray())).List();

                    if (task.Id.HasValue)
                    {
                        IList <TaskComment> currentCommentsEntitities = session.QueryOver <TaskComment>()
                                                                        .JoinQueryOver(c => c.Task)
                                                                        .Where(t => t.Id == task.Id.Value)
                                                                        .List();

                        foreach (var comment in currentCommentsEntitities)
                        {
                            session.Delete(comment);
                            session.Flush();
                        }
                    }

                    var commentsEntities = new List <TaskComment>();
                    foreach (var c in task.Comments.OrderBy(c => c.Date))
                    {
                        commentsEntities.Add(new TaskComment
                        {
                            Content = c.Content,
                            Date    = c.Date,
                            User    = session.QueryOver <User>().Where(u => u.Id == c.AuthorId).SingleOrDefault(),
                            Task    = taskEntity
                        });
                    }

                    taskEntity.Comments = commentsEntities;

                    session.SaveOrUpdate(taskEntity);
                    session.Flush();
                    transaction.Commit();

                    return(task.Id.HasValue ? TaskCreationStatus.Modified : TaskCreationStatus.Added);
                }
            }
        }