示例#1
0
        private void Insertwithtxn(List <string> sqls)
        {
            var tran = _rDBService.BeginTransaction();

            try
            {
                foreach (var sql in sqls)
                {
                    if (!_rDBService.WriteData(sql, null, tran))
                    {
                        _rDBService.RollbackTransaction(tran);
                        return;
                    }
                }

                _rDBService.CommitTransaction(tran);
            }
            catch (Exception)
            {
                _rDBService.RollbackTransaction(tran);
                throw;
            }
        }
示例#2
0
        public override async Task <bool> CreateUserAsync(UserModel user, bool sendEmail = true)
        {
            user.user_name = user.user_name.ToLower();
            if (user != null && !IsExistsByUserName(user.user_name))
            {
                var dbtxn = _rdBService.BeginTransaction();
                try
                {
                    if (user.roles == null)
                    {
                        user.roles = new List <string>();
                    }
                    var roles = new List <string>()
                    {
                        "user", "init_user"
                    };
                    roles.AddRange(user.roles);
                    user.roles = roles.Distinct().ToList();
                    user.salt  = CommonUtility.RandomString(10);
                    user.email = user.email?.ToLower();
                    var dbroles = GetAndAddDbValues <RoleDbo>(dbtxn, user.roles, "role", (d) =>
                    {
                        return(user.roles.IndexOf(d.name) != -1);
                    },
                                                              (d, r) =>
                    {
                        return(d.name == r);
                    }
                                                              , (d) =>
                    {
                        return(new RoleDbo()
                        {
                            is_enabled = true,
                            name = d
                        });
                    });
                    var usertypes = new List <string>()
                    {
                        user.user_type
                    };
                    var dbauthtype = GetAndAddDbValues <UserAuthTypeDbo>(dbtxn, usertypes, IdentityTable.USER_AUTH_TYPE, (d) =>
                    {
                        return(usertypes.IndexOf(d.name) != -1);
                    },
                                                                         (d, r) =>
                    {
                        return(d.name == r);
                    }
                                                                         , (d) =>
                    {
                        return(new UserAuthTypeDbo()
                        {
                            is_enabled = true,
                            name = d
                        });
                    });


                    var usermodel = new UserModelDbo()
                    {
                        is_enabled        = true,
                        first_name        = user.first_name,
                        middle_name       = user.middle_name,
                        last_name         = user.last_name,
                        user_name         = user.user_name,
                        email             = user.email,
                        salt              = user.salt,
                        user_auth_type_id = dbauthtype.First().user_auth_type_id
                    };


                    var dbuserid = _rdBService.WriteData <UserModelDbo>(usermodel, dbtxn);
                    var userinfo = new UserProfileDbo()
                    {
                        user_id      = dbuserid,
                        phone_number = ""
                    };
                    _rdBService.WriteData <UserProfileDbo>(userinfo, dbtxn);
                    user.user_id = dbuserid.ToString();
                    foreach (var role in dbroles)
                    {
                        _rdBService.WriteData <UserRoleDbo>(new UserRoleDbo()
                        {
                            is_enabled = true,
                            user_id    = dbuserid,
                            role_id    = role.role_id
                        }, dbtxn);
                    }
                    if (AddUserToTenants(user))
                    {
                        _rdBService.CommitTransaction(dbtxn);

                        if (sendEmail)
                        {
                            await _userNotifierService.SendWelcomeEmailAsync(user);
                        }

                        return(await Task.FromResult(true));
                    }
                    else
                    {
                        return(await Task.FromResult(false));
                    }
                }
                catch (Exception)
                {
                    _rdBService.RollbackTransaction(dbtxn);
                    return(await Task.FromResult(false));
                }
            }
            return(await Task.FromResult(false));
        }