public static OrganizationUser OrganizationUserFetch(int organizationUserId)
 {
     return(OrganizationUser.FetchOrganizationUser(
                new OrganizationUserMemberDataCriteria
     {
         OrganizationUserMemberId = organizationUserId
     }));
 }
        internal static OrganizationUser FetchOrganizationUser(OrganizationUserMemberData data)
        {
            var result = new OrganizationUser();

            result.Fetch(data);
            result.MarkOld();

            return result;
        }
        internal static OrganizationUser FetchOrganizationUser(OrganizationUserMemberData data)
        {
            var result = new OrganizationUser();

            result.Fetch(data);
            result.MarkOld();

            return(result);
        }
        public static OrganizationUser OrganizationUserAdd(int organizationId, int userId, Role role)
        {
            var organizationUser = OrganizationUser.NewOrganizationUser(organizationId, userId);

            organizationUser.RoleId = (int)role;

            organizationUser = OrganizationUserRepository.OrganizationUserSave(organizationUser);

            return(organizationUser);
        }
        public static OrganizationUser OrganizationUserUpdate(OrganizationUser organizationUser)
        {
            if (!organizationUser.IsDirty)
            {
                return(organizationUser);
            }

            organizationUser = organizationUser.Save();

            return(organizationUser);
        }
        internal static OrganizationUser OrganizationUserAdd(int organizationId, int userId, Role role, bool ignoreAuthorization)
        {
            var organizationUser = OrganizationUser.NewOrganizationUser(organizationId, userId);

            organizationUser.RoleId = (int)role;

            if (ignoreAuthorization)
            {
                organizationUser = OrganizationUserRepository.OrganizationUserInsert(organizationUser);
            }
            else
            {
                organizationUser = OrganizationUserRepository.OrganizationUserSave(organizationUser);
            }

            return(organizationUser);
        }
        public static bool OrganizationUserDelete(OrganizationUser organizationUser)
        {
            OrganizationUserRepository.AuthorizeOrganizationUser(organizationUser.OrganizationId);

            if (OrganizationUserRepository.OrganizationUserFetch(
                organizationUser.OrganizationId, organizationUser.UserId).RoleId == (int)Role.Owner)
            {
                throw new NotSupportedException("You cannot delete the owner of a organization");
            }

            OrganizationUser.DeleteOrganizationUser(
                new OrganizationUserMemberDataCriteria
                {
                    OrganizationUserMemberId = organizationUser.OrganizationUserMemberId
                });

            return true;
        }
        public static bool OrganizationUserDelete(OrganizationUser organizationUser)
        {
            OrganizationUserRepository.AuthorizeOrganizationUser(organizationUser.OrganizationId);

            if (OrganizationUserRepository.OrganizationUserFetch(
                    organizationUser.OrganizationId, organizationUser.UserId).RoleId == (int)Role.Owner)
            {
                throw new NotSupportedException("You cannot delete the owner of a organization");
            }

            OrganizationUser.DeleteOrganizationUser(
                new OrganizationUserMemberDataCriteria
            {
                OrganizationUserMemberId = organizationUser.OrganizationUserMemberId
            });

            return(true);
        }
        public static OrganizationUser OrganizationUserSave(OrganizationUser organizationUser)
        {
            if (!organizationUser.IsValid)
            {
                return(organizationUser);
            }

            OrganizationUserRepository.AuthorizeOrganizationUser(organizationUser.OrganizationId);

            OrganizationUser result;

            if (organizationUser.IsNew)
            {
                result = OrganizationUserRepository.OrganizationUserInsert(organizationUser);
            }
            else
            {
                result = OrganizationUserRepository.OrganizationUserUpdate(organizationUser);
            }

            return(result);
        }
        public static OrganizationUser OrganizationUserUpdate(OrganizationUser organizationUser)
        {
            if (!organizationUser.IsDirty)
            {
                return organizationUser;
            }

            organizationUser = organizationUser.Save();

            return organizationUser;
        }
        public static OrganizationUser OrganizationUserSave(OrganizationUser organizationUser)
        {
            if (!organizationUser.IsValid)
            {
                return organizationUser;
            }

            OrganizationUserRepository.AuthorizeOrganizationUser(organizationUser.OrganizationId);

            OrganizationUser result;

            if (organizationUser.IsNew)
            {
                result = OrganizationUserRepository.OrganizationUserInsert(organizationUser);
            }
            else
            {
                result = OrganizationUserRepository.OrganizationUserUpdate(organizationUser);
            }

            return result;
        }
        public static OrganizationUser OrganizationUserInsert(OrganizationUser organizationUser)
        {
            organizationUser = organizationUser.Save();

            return organizationUser;
        }
        public static OrganizationUser OrganizationUserNew(int organizationId, int userId)
        {
            var organizationUser = OrganizationUser.NewOrganizationUser(organizationId, userId);

            return(organizationUser);
        }
        public static OrganizationUser OrganizationUserInsert(OrganizationUser organizationUser)
        {
            organizationUser = organizationUser.Save();

            return(organizationUser);
        }