public List <ServerDomainGroupData> GetMailGroups()
        {
            if (!IsAdmin)
            {
                throw new SecurityException("Need admin privileges.");
            }

            var list = new List <ServerDomainGroupData>();

            using (var daoFactory = new DaoFactory())
            {
                var serverDomainDao = daoFactory.CreateServerDomainDao(Tenant);

                var domains = serverDomainDao.GetDomains();

                var serverGroupDao = daoFactory.CreateServerGroupDao(Tenant);

                var groups = serverGroupDao.GetList();

                var serverAddressDao = daoFactory.CreateServerAddressDao(Tenant);

                list.AddRange(from serverGroup in groups
                              let address = serverAddressDao.Get(serverGroup.AddressId)
                                            let domain = domains.FirstOrDefault(d => d.Id == address.DomainId)
                                                         where domain != null
                                                         let serverGroupAddress = ServerMailboxEngine.ToServerDomainAddressData(address, domain)
                                                                                  let serverGroupAddresses =
                                  serverAddressDao.GetGroupAddresses(serverGroup.Id)
                                  .ConvertAll(a => ServerMailboxEngine.ToServerDomainAddressData(a, domain))
                                  select ToServerDomainGroupData(serverGroup.Id, serverGroupAddress, serverGroupAddresses));
            }

            return(list);
        }
Exemplo n.º 2
0
        public ServerFullData GetMailServerFullInfo()
        {
            if (!IsAdmin)
            {
                throw new SecurityException("Need admin privileges.");
            }

            var fullServerInfo    = new ServerFullData();
            var mailboxDataList   = new List <ServerMailboxData>();
            var mailgroupDataList = new List <ServerDomainGroupData>();

            var domainEngine = new ServerDomainEngine(Tenant, User);

            using (var daoFactory = new DaoFactory())
            {
                var server = GetMailServer(daoFactory);

                var domains = domainEngine.GetDomains(daoFactory);

                var mailboxDao = daoFactory.CreateMailboxDao();

                var mailboxes = mailboxDao.GetMailBoxes(new TenantServerMailboxesExp(Tenant));

                var serverAddressDao = daoFactory.CreateServerAddressDao(Tenant);

                var addresses = serverAddressDao.GetList();

                foreach (var mailbox in mailboxes)
                {
                    var address =
                        addresses.FirstOrDefault(
                            a => a.MailboxId == mailbox.Id && a.IsAlias == false && a.IsMailGroup == false);

                    if (address == null)
                    {
                        continue;
                    }

                    var domain = domains.FirstOrDefault(d => d.Id == address.DomainId);

                    if (domain == null)
                    {
                        continue;
                    }

                    var serverAddressData = ServerMailboxEngine.ToServerDomainAddressData(address, domain);

                    var aliases =
                        addresses.Where(a => a.MailboxId == mailbox.Id && a.IsAlias && !a.IsMailGroup)
                        .ToList()
                        .ConvertAll(a => ServerMailboxEngine.ToServerDomainAddressData(a, domain));

                    mailboxDataList.Add(ServerMailboxEngine.ToMailboxData(mailbox, serverAddressData, aliases));
                }

                var serverGroupDao = daoFactory.CreateServerGroupDao(Tenant);

                var groups = serverGroupDao.GetList();

                foreach (var serverGroup in groups)
                {
                    var address =
                        addresses.FirstOrDefault(
                            a => a.Id == serverGroup.AddressId && !a.IsAlias && a.IsMailGroup);

                    if (address == null)
                    {
                        continue;
                    }

                    var domain = domains.FirstOrDefault(d => d.Id == address.DomainId);

                    if (domain == null)
                    {
                        continue;
                    }

                    var email = string.Format("{0}@{1}", address.AddressName, domain.Name);

                    var serverGroupAddress = ServerMailboxEngine.ToServerDomainAddressData(address, email);

                    var serverGroupAddresses =
                        serverAddressDao.GetGroupAddresses(serverGroup.Id)
                        .ConvertAll(a => ServerMailboxEngine.ToServerDomainAddressData(a,
                                                                                       string.Format("{0}@{1}", a.AddressName, domain.Name)));

                    mailgroupDataList.Add(ServerMailgroupEngine.ToServerDomainGroupData(serverGroup.Id, serverGroupAddress, serverGroupAddresses));
                }

                fullServerInfo.Server     = server;
                fullServerInfo.Domains    = domains;
                fullServerInfo.Mailboxes  = mailboxDataList;
                fullServerInfo.Mailgroups = mailgroupDataList;
            }

            return(fullServerInfo);
        }
        public ServerDomainGroupData CreateMailGroup(string name, int domainId, List <int> addressIds)
        {
            if (!IsAdmin)
            {
                throw new SecurityException("Need admin privileges.");
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(@"Invalid mailgroup name.", "name");
            }

            if (domainId < 0)
            {
                throw new ArgumentException(@"Invalid domain id.", "domainId");
            }

            if (name.Length > 64)
            {
                throw new ArgumentException(@"Local part of mailgroup exceed limitation of 64 characters.", "name");
            }

            if (!Parser.IsEmailLocalPartValid(name))
            {
                throw new ArgumentException(@"Incorrect group name.", "name");
            }

            if (!addressIds.Any())
            {
                throw new ArgumentException(@"Empty collection of address_ids.", "addressIds");
            }

            var mailgroupName = name.ToLowerInvariant();

            using (var daoFactory = new DaoFactory())
            {
                var serverDomainDao = daoFactory.CreateServerDomainDao(Tenant);

                var serverDomain = serverDomainDao.GetDomain(domainId);

                if (serverDomain.Tenant == Defines.SHARED_TENANT_ID)
                {
                    throw new InvalidOperationException("Creating mail group is not allowed for shared domain.");
                }

                var serverAddressDao = daoFactory.CreateServerAddressDao(Tenant);

                if (serverAddressDao.IsAddressAlreadyRegistered(mailgroupName, serverDomain.Name))
                {
                    throw new DuplicateNameException("You want to create a group with already existing address.");
                }

                var utcNow = DateTime.UtcNow;

                var address = new ServerAddress
                {
                    Id          = 0,
                    Tenant      = Tenant,
                    MailboxId   = -1,
                    DomainId    = serverDomain.Id,
                    AddressName = mailgroupName,
                    IsAlias     = false,
                    IsMailGroup = true,
                    DateCreated = utcNow
                };

                var groupEmail = string.Format("{0}@{1}", mailgroupName, serverDomain.Name);

                var groupAddressData = ServerMailboxEngine.ToServerDomainAddressData(address, groupEmail);

                var newGroupMembers = serverAddressDao.GetList(addressIds);

                var newGroupMemberIds = newGroupMembers.ConvertAll(m => m.Id);

                var newGroupMemberDataList =
                    newGroupMembers.ConvertAll(m =>
                                               ServerMailboxEngine.ToServerDomainAddressData(m,
                                                                                             string.Format("{0}@{1}", m.AddressName, serverDomain.Name)));

                var goTo = string.Join(",",
                                       newGroupMembers.Select(m => string.Format("{0}@{1}", m.AddressName, serverDomain.Name)));

                var serverDao = daoFactory.CreateServerDao();
                var server    = serverDao.Get(Tenant);

                var engine = new Server.Core.ServerEngine(server.Id, server.ConnectionString);

                var group = new ServerGroup
                {
                    Id          = 0,
                    Tenant      = Tenant,
                    Address     = groupEmail,
                    AddressId   = 0,
                    DateCreated = utcNow
                };

                using (var tx = daoFactory.DbManager.BeginTransaction(IsolationLevel.ReadUncommitted))
                {
                    address.Id = serverAddressDao.Save(address);

                    group.AddressId = address.Id;

                    var serverGroupDao = daoFactory.CreateServerGroupDao(Tenant);

                    group.Id = serverGroupDao.Save(group);

                    serverAddressDao.AddAddressesToMailGroup(group.Id, newGroupMemberIds);

                    var serverAddress = new Alias
                    {
                        Name     = "",
                        Address  = groupEmail,
                        GoTo     = goTo,
                        Domain   = serverDomain.Name,
                        IsActive = true,
                        IsGroup  = true,
                        Modified = utcNow,
                        Created  = utcNow
                    };

                    engine.SaveAlias(serverAddress);

                    tx.Commit();
                }

                CacheEngine.ClearAll();

                return(ToServerDomainGroupData(group.Id, groupAddressData, newGroupMemberDataList));
            }
        }
        public ServerDomainGroupData AddMailGroupMember(int mailgroupId, int addressId)
        {
            if (!IsAdmin)
            {
                throw new SecurityException("Need admin privileges.");
            }

            if (addressId < 0)
            {
                throw new ArgumentException(@"Invalid address id.", "addressId");
            }

            if (mailgroupId < 0)
            {
                throw new ArgumentException(@"Invalid mailgroup id.", "mailgroupId");
            }

            using (var daoFactory = new DaoFactory())
            {
                var serverGroupDao = daoFactory.CreateServerGroupDao(Tenant);

                var group = serverGroupDao.Get(mailgroupId);

                if (group == null)
                {
                    throw new Exception("Group not found");
                }

                var serverAddressDao = daoFactory.CreateServerAddressDao(Tenant);

                var groupMembers = serverAddressDao.GetGroupAddresses(mailgroupId);

                if (groupMembers.Exists(a => a.Id == addressId))
                {
                    throw new DuplicateNameException("Member already exists");
                }

                var newMemberAddress = serverAddressDao.Get(addressId);

                if (newMemberAddress == null)
                {
                    throw new Exception("Member not found");
                }

                var serverDao = daoFactory.CreateServerDao();
                var server    = serverDao.Get(Tenant);

                var engine = new Server.Core.ServerEngine(server.Id, server.ConnectionString);

                var utcNow = DateTime.UtcNow;

                ServerAddress groupAddress;
                string        groupEmail;
                List <ServerDomainAddressData> newGroupMemberDataList;

                using (var tx = daoFactory.DbManager.BeginTransaction(IsolationLevel.ReadUncommitted))
                {
                    serverAddressDao.AddAddressesToMailGroup(mailgroupId, new List <int> {
                        addressId
                    });

                    groupMembers.Add(newMemberAddress);

                    groupAddress = serverAddressDao.Get(group.AddressId);

                    var serverDomainDao = daoFactory.CreateServerDomainDao(Tenant);

                    var serverDomain = serverDomainDao.GetDomain(groupAddress.DomainId);

                    var goTo = string.Join(",",
                                           groupMembers.Select(m => string.Format("{0}@{1}", m.AddressName, serverDomain.Name)));

                    groupEmail = string.Format("{0}@{1}", groupAddress.AddressName, serverDomain.Name);

                    newGroupMemberDataList =
                        groupMembers.ConvertAll(m =>
                                                ServerMailboxEngine.ToServerDomainAddressData(m,
                                                                                              string.Format("{0}@{1}", m.AddressName, serverDomain.Name)));

                    var serverAddress = new Alias
                    {
                        Name     = "",
                        Address  = groupEmail,
                        GoTo     = goTo,
                        Domain   = serverDomain.Name,
                        IsActive = true,
                        IsGroup  = true,
                        Modified = utcNow,
                        Created  = utcNow
                    };

                    engine.SaveAlias(serverAddress);

                    tx.Commit();
                }

                var groupAddressData = ServerMailboxEngine.ToServerDomainAddressData(groupAddress, groupEmail);

                CacheEngine.ClearAll();

                return(ToServerDomainGroupData(group.Id, groupAddressData, newGroupMemberDataList));
            }
        }