public MailboxWithAddressDto CreateMailbox(IAccount teamlabAccount, string name,
                                                   string fullAddress, string password, string addressName,
                                                   DateTime addressCreatedDate,
                                                   int domainId, string domainName, bool isVerified, DbManager db)
        {
            if (teamlabAccount == null)
            {
                throw new ArgumentNullException("teamlabAccount");
            }

            if (string.IsNullOrEmpty(fullAddress))
            {
                throw new ArgumentNullException("fullAddress");
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            if (string.IsNullOrEmpty(addressName))
            {
                throw new ArgumentNullException("addressName");
            }

            if (domainId < 0)
            {
                throw new ArgumentException("Argument domain_id less then zero.", "domainId");
            }

            if (string.IsNullOrEmpty(domainName))
            {
                throw new ArgumentNullException("domainName");
            }

            if (db == null)
            {
                throw new ArgumentNullException("db");
            }

            const string mailbox_alias = "mb";
            const string address_alias = "a";

            var userTeamlabMailboxCountQuery = new SqlQuery(MailboxTable.Name.Alias(mailbox_alias))
                                               .InnerJoin(AddressTable.Name.Alias(address_alias),
                                                          Exp.EqColumns(MailboxTable.Columns.Id.Prefix(mailbox_alias),
                                                                        AddressTable.Columns.MailboxId.Prefix(address_alias)))
                                               .SelectCount()
                                               .Where(MailboxTable.Columns.IsTeamlabMailbox.Prefix(mailbox_alias), true)
                                               .Where(MailboxTable.Columns.IsRemoved.Prefix(mailbox_alias), false)
                                               .Where(MailboxTable.Columns.Tenant.Prefix(mailbox_alias), tenant)
                                               .Where(MailboxTable.Columns.User.Prefix(mailbox_alias), teamlabAccount.ID.ToString())
                                               .Where(AddressTable.Columns.DomainId.Prefix(address_alias), domainId)
                                               .Where(AddressTable.Columns.IsAlias.Prefix(address_alias), false);

            var userMailboxesCount = db.ExecuteScalar <int>(userTeamlabMailboxCountQuery);

            if (userMailboxesCount >= _mailboxCountLimit)
            {
                throw new ArgumentOutOfRangeException("addressName", string.Format("Count of user's mailboxes must be less or equal {0}.", _mailboxCountLimit));
            }

            var serverInformation = GetTenantServer(db);

            var dateCreated = DateTime.UtcNow;

            var insertQuery = new SqlInsert(MailboxTable.Name)
                              .InColumnValue(MailboxTable.Columns.Id, 0)
                              .InColumnValue(MailboxTable.Columns.Tenant, tenant)
                              .InColumnValue(MailboxTable.Columns.User, teamlabAccount.ID.ToString())
                              .InColumnValue(MailboxTable.Columns.Address, fullAddress)
                              .InColumnValue(MailboxTable.Columns.AddressName, name)
                              .InColumnValue(MailboxTable.Columns.Password, InstanceCrypto.Encrypt(password))
                              .InColumnValue(MailboxTable.Columns.MsgCountLast, 0)
                              .InColumnValue(MailboxTable.Columns.SmtpPassword, InstanceCrypto.Encrypt(password))
                              .InColumnValue(MailboxTable.Columns.SizeLast, 0)
                              .InColumnValue(MailboxTable.Columns.LoginDelay, Config.LoginDelayInSeconds)
                              .InColumnValue(MailboxTable.Columns.Enabled, true)
                              .InColumnValue(MailboxTable.Columns.Imap, true)
                              .InColumnValue(MailboxTable.Columns.OAuthType, AuthorizationServiceType.None)
                              .InColumnValue(MailboxTable.Columns.OAuthToken, null)
                              .InColumnValue(MailboxTable.Columns.DateCreated, dateCreated)
                              .InColumnValue(MailboxTable.Columns.SmtpServerId, serverInformation.smtp_settings_id)
                              .InColumnValue(MailboxTable.Columns.ServerId, serverInformation.imap_settings_id)
                              .InColumnValue(MailboxTable.Columns.IsTeamlabMailbox, true)
                              .Identity(0, 0, true);


            var result = db.ExecuteScalar <int>(insertQuery);

            var createdMailbox = new MailboxDto(result, teamlabAccount.ID.ToString(), tenant,
                                                fullAddress, name);

            var addressDal = new MailAddressDal(tenant);

            var mailboxAddress = addressDal.AddMailboxAddress(createdMailbox.id, addressName, addressCreatedDate,
                                                              domainId, domainName, isVerified, db);

            var resultDto = new MailboxWithAddressDto(createdMailbox, mailboxAddress);

            return(resultDto);
        }
Exemple #2
0
        public MailboxWithAddressDto CreateMailbox(IAccount teamlabAccount,
                                                   string fullAddress, string password, string addressName,
                                                   DateTime addressCreatedDate,
                                                   int domainId, string domainName, bool isVerified, DbManager db)
        {
            if (teamlabAccount == null)
                throw new ArgumentNullException("teamlabAccount");

            if(string.IsNullOrEmpty(fullAddress))
                throw new ArgumentNullException("fullAddress");

            if (string.IsNullOrEmpty(password))
                throw new ArgumentNullException("password");

            if (string.IsNullOrEmpty(addressName))
                throw new ArgumentNullException("addressName");

            if (domainId < 0)
                throw new ArgumentException("Argument domain_id less then zero.", "domainId");

            if (string.IsNullOrEmpty(domainName))
                throw new ArgumentNullException("domainName");

            if (db == null)
                throw new ArgumentNullException("db");

            const string mailbox_alias = "mb";
            const string address_alias = "a";

            var userTeamlabMailboxCountQuery = new SqlQuery(MailboxTable.name.Alias(mailbox_alias))
                .InnerJoin(AddressTable.name.Alias(address_alias),
                           Exp.EqColumns(MailboxTable.Columns.id.Prefix(mailbox_alias),
                                         AddressTable.Columns.id_mailbox.Prefix(address_alias)))
                .SelectCount()
                .Where(MailboxTable.Columns.is_teamlab_mailbox.Prefix(mailbox_alias), true)
                .Where(MailboxTable.Columns.is_removed.Prefix(mailbox_alias), false)
                .Where(MailboxTable.Columns.id_tenant.Prefix(mailbox_alias), tenant)
                .Where(MailboxTable.Columns.id_user.Prefix(mailbox_alias), teamlabAccount.ID.ToString())
                .Where(AddressTable.Columns.id_domain.Prefix(address_alias), domainId)
                .Where(AddressTable.Columns.is_alias.Prefix(address_alias), false);

            var userMailboxesCount = db.ExecuteScalar<int>(userTeamlabMailboxCountQuery);

            if (userMailboxesCount >= _mailboxCountLimit)
                throw new ArgumentOutOfRangeException("addressName", string.Format("Count of user's mailboxes must be less or equal {0}.", _mailboxCountLimit));

            var serverInformation = GetTenantServer(db);

            var dateCreated = DateTime.UtcNow.ToDbStyle();

            var insertQuery = new SqlInsert(MailboxTable.name)
                .InColumnValue(MailboxTable.Columns.id, 0)
                .InColumnValue(MailboxTable.Columns.id_tenant, tenant)
                .InColumnValue(MailboxTable.Columns.id_user, teamlabAccount.ID.ToString())
                .InColumnValue(MailboxTable.Columns.address, fullAddress)
                .InColumnValue(MailboxTable.Columns.name, teamlabAccount.Name)
                .InColumnValue(MailboxTable.Columns.password, InstanceCrypto.Encrypt(password))
                .InColumnValue(MailboxTable.Columns.msg_count_last, 0)
                .InColumnValue(MailboxTable.Columns.smtp_password, InstanceCrypto.Encrypt(password))
                .InColumnValue(MailboxTable.Columns.size_last, 0)
                .InColumnValue(MailboxTable.Columns.login_delay, Config.LoginDelayInSeconds)
                .InColumnValue(MailboxTable.Columns.enabled, true)
                .InColumnValue(MailboxTable.Columns.imap, true)
                .InColumnValue(MailboxTable.Columns.service_type, 0)
                .InColumnValue(MailboxTable.Columns.refresh_token, null)
                .InColumnValue(MailboxTable.Columns.date_created, dateCreated)
                .InColumnValue(MailboxTable.Columns.id_smtp_server, serverInformation.smtp_settings_id)
                .InColumnValue(MailboxTable.Columns.id_in_server, serverInformation.imap_settings_id)
                .InColumnValue(MailboxTable.Columns.is_teamlab_mailbox, true)
                .Identity(0, 0, true);


            var result = db.ExecuteScalar<int>(insertQuery);

            var createdMailbox = new MailboxDto(result, teamlabAccount.ID.ToString(), tenant,
                                                 fullAddress);

            var addressDal = new MailAddressDal(tenant);

            var mailboxAddress = addressDal.AddMailboxAddress(createdMailbox.id, addressName, addressCreatedDate,
                                                    domainId, domainName, isVerified, db);

            var resultDto = new MailboxWithAddressDto(createdMailbox, mailboxAddress);

            return resultDto;
        }
        public MailboxWithAddressDto CreateMailbox(IAccount teamlab_account,
                                                   string full_address, string password, string address_name,
                                                   DateTime address_created_date,
                                                   int domain_id, string domain_name, bool is_verified, DbManager db)
        {
            if (teamlab_account == null)
            {
                throw new ArgumentNullException("teamlab_account");
            }

            if (string.IsNullOrEmpty(full_address))
            {
                throw new ArgumentNullException("full_address");
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }

            if (string.IsNullOrEmpty(address_name))
            {
                throw new ArgumentNullException("address_name");
            }

            if (domain_id < 0)
            {
                throw new ArgumentException("Argument domain_id less then zero.", "domain_id");
            }

            if (string.IsNullOrEmpty(domain_name))
            {
                throw new ArgumentNullException("domain_name");
            }

            if (db == null)
            {
                throw new ArgumentNullException("db");
            }

            const string mailbox_alias = "mb";
            const string address_alias = "a";

            var user_teamlab_mailbox_count_query = new SqlQuery(MailboxTable.name.Alias(mailbox_alias))
                                                   .InnerJoin(AddressTable.name.Alias(address_alias),
                                                              Exp.EqColumns(MailboxTable.Columns.id.Prefix(mailbox_alias),
                                                                            AddressTable.Columns.id_mailbox.Prefix(address_alias)))
                                                   .SelectCount()
                                                   .Where(MailboxTable.Columns.is_teamlab_mailbox.Prefix(mailbox_alias), true)
                                                   .Where(MailboxTable.Columns.is_removed.Prefix(mailbox_alias), false)
                                                   .Where(MailboxTable.Columns.id_tenant.Prefix(mailbox_alias), tenant_id)
                                                   .Where(MailboxTable.Columns.id_user.Prefix(mailbox_alias), teamlab_account.ID.ToString())
                                                   .Where(AddressTable.Columns.id_domain.Prefix(address_alias), domain_id)
                                                   .Where(AddressTable.Columns.is_alias.Prefix(address_alias), false);

            var user_mailboxes_count = db.ExecuteScalar <int>(user_teamlab_mailbox_count_query);

            if (user_mailboxes_count >= _mailboxCountLimit)
            {
                throw new ArgumentOutOfRangeException("address_name", string.Format("Count of user's mailboxes must be less or equal {0}.", _mailboxCountLimit));
            }

            var server_information = GetTenantServer(db);

            var date_created = DateTime.UtcNow.ToDbStyle();

            var insert_query = new SqlInsert(MailboxTable.name)
                               .InColumnValue(MailboxTable.Columns.id, 0)
                               .InColumnValue(MailboxTable.Columns.id_tenant, tenant_id)
                               .InColumnValue(MailboxTable.Columns.id_user, teamlab_account.ID.ToString())
                               .InColumnValue(MailboxTable.Columns.address, full_address) //Todo: Talk with AK about this.
                               .InColumnValue(MailboxTable.Columns.name, teamlab_account.Name)
                               .InColumnValue(MailboxTable.Columns.password, InstanceCrypto.Encrypt(password))
                               .InColumnValue(MailboxTable.Columns.msg_count_last, 0)
                               .InColumnValue(MailboxTable.Columns.smtp_password, InstanceCrypto.Encrypt(password))
                               .InColumnValue(MailboxTable.Columns.size_last, 0)
                               .InColumnValue(MailboxTable.Columns.login_delay, Config.LoginDelayInSeconds)
                               .InColumnValue(MailboxTable.Columns.enabled, true)
                               .InColumnValue(MailboxTable.Columns.imap, true)
                               .InColumnValue(MailboxTable.Columns.service_type, 0)
                               .InColumnValue(MailboxTable.Columns.refresh_token, null)
                               .InColumnValue(MailboxTable.Columns.date_created, date_created)
                               .InColumnValue(MailboxTable.Columns.id_smtp_server, server_information.smtp_settings_id)
                               .InColumnValue(MailboxTable.Columns.id_in_server, server_information.imap_settings_id)
                               .InColumnValue(MailboxTable.Columns.is_teamlab_mailbox, true)
                               .Identity(0, 0, true);


            var result = db.ExecuteScalar <int>(insert_query);

            var created_mailbox = new MailboxDto(result, teamlab_account.ID.ToString(), tenant_id,
                                                 full_address);

            var address_dal = new MailAddressDal(tenant_id);

            var mailbox_address = address_dal.AddMailboxAddress(created_mailbox.id, address_name, address_created_date,
                                                                domain_id, domain_name, is_verified, db);

            var result_dto = new MailboxWithAddressDto(created_mailbox, mailbox_address);

            return(result_dto);
        }