예제 #1
0
        public async Task <IdentityResult> RigsterUser(OrphanageDataModel.Persons.User userModel)
        {
            IdentityUser user = new IdentityUser
            {
                UserName = userModel.UserName
            };

            var result = await _userManager.CreateAsync(user, userModel.Password);

            return(result);
        }
예제 #2
0
        public async Task <HttpResponseMessage> Post(object guarantor)
        {
            var userEntity = JsonConvert.DeserializeObject <OrphanageDataModel.Persons.User>(guarantor.ToString());

            OrphanageDataModel.Persons.User ret = null;

            ret = await _userDBService.AddUser(userEntity);

            if (ret != null)
            {
                return(Request.CreateResponse(System.Net.HttpStatusCode.Created, ret));
            }
            else
            {
                return(_httpResponseMessageConfiguerer.NothingChanged());
            }
        }
예제 #3
0
        private ClaimsIdentity setClaimsIdentity(OrphanageDataModel.Persons.User user, string AuthenticationType)
        {
            var identity = new ClaimsIdentity(AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            if (user.IsAdmin)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
                identity.AddClaim(new Claim(ClaimTypes.Role, "CanAdd"));
                identity.AddClaim(new Claim(ClaimTypes.Role, "CanDelete"));
                identity.AddClaim(new Claim(ClaimTypes.Role, "CanDeposit"));
                identity.AddClaim(new Claim(ClaimTypes.Role, "CanDraw"));
                identity.AddClaim(new Claim(ClaimTypes.Role, "CanRead"));
            }
            else
            {
                if (user.CanAdd)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "CanAdd"));
                }
                if (user.CanDelete)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "CanDelete"));
                }
                if (user.CanDeposit)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "CanDeposit"));
                }
                if (user.CanDraw)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "CanDraw"));
                }
                if (user.CanRead)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "CanRead"));
                }
            }

            return(identity);
        }
예제 #4
0
        public async Task <bool> SaveUser(OrphanageDataModel.Persons.User user)
        {
            _logger.Information($"Trying to save user");
            if (user == null)
            {
                _logger.Error($"the parameter object user is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (user.UserName == null || user.UserName.Length == 0)
            {
                _logger.Error($"the UserName of the parameter object user equals {user.UserName}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (OrphanageDbCNoBinary orphanageDc = new OrphanageDbCNoBinary())
            {
                int ret = 0;
                orphanageDc.Configuration.LazyLoadingEnabled       = true;
                orphanageDc.Configuration.ProxyCreationEnabled     = true;
                orphanageDc.Configuration.AutoDetectChangesEnabled = true;

                var orginalUser = await orphanageDc.Users.
                                  Include(m => m.Address).
                                  Include(c => c.Name).
                                  FirstOrDefaultAsync(m => m.Id == user.Id);

                if (orginalUser == null)
                {
                    _logger.Error($"the original user object with id {user.Id} object is not founded, ObjectNotFoundException will be thrown");
                    throw new Exceptions.ObjectNotFoundException();
                }

                _logger.Information($"processing the address object of the user with id({user.Id})");
                if (user.Address != null)
                {
                    if (orginalUser.Address != null)
                    {
                        //edit existing user address
                        ret += await _regularDataService.SaveAddress(user.Address, orphanageDc);
                    }
                    else
                    {
                        //create new address for the user
                        var addressId = await _regularDataService.AddAddress(user.Address, orphanageDc);

                        orginalUser.AddressId = addressId;
                        ret++;
                    }
                }
                else
                {
                    if (orginalUser.Address != null)
                    {
                        //delete existing user address
                        int alAdd = orginalUser.AddressId.Value;
                        orginalUser.AddressId = null;
                        await orphanageDc.SaveChangesAsync();

                        await _regularDataService.DeleteAddress(alAdd, orphanageDc);
                    }
                }
                if (user.Name != null)
                {
                    if (orginalUser.Name != null)
                    {
                        //edit existing user name
                        ret += await _regularDataService.SaveName(user.Name, orphanageDc);
                    }
                    else
                    {
                        //create new name for the user
                        var nameId = await _regularDataService.AddName(user.Name, orphanageDc);

                        orginalUser.NameId = nameId;
                        ret++;
                    }
                }
                else
                {
                    if (orginalUser.Name != null)
                    {
                        //delete existing user name
                        int alAdd = orginalUser.NameId.Value;
                        orginalUser.NameId = null;
                        await orphanageDc.SaveChangesAsync();

                        await _regularDataService.DeleteName(alAdd, orphanageDc);
                    }
                }
                orginalUser.CanAdd     = user.CanAdd;
                orginalUser.CanDelete  = user.CanDelete;
                orginalUser.CanDeposit = user.CanDeposit;
                orginalUser.CanDraw    = user.CanDraw;
                orginalUser.CanRead    = user.CanRead;
                orginalUser.IsAdmin    = user.IsAdmin;
                orginalUser.Password   = _passwordHasher.Hash(user.Password);
                orginalUser.UserName   = user.UserName;
                orginalUser.Note       = user.Note;
                ret += await orphanageDc.SaveChangesAsync();

                if (ret > 0)
                {
                    _logger.Information($"user with id({user.Id}) has been successfully saved to the database, {ret} changes have been made");
                    return(true);
                }
                else
                {
                    _logger.Information($"nothing has changed, false will be returned");
                    return(false);
                }
            }
        }
예제 #5
0
        public async Task <OrphanageDataModel.Persons.User> AddUser(OrphanageDataModel.Persons.User user)
        {
            _logger.Information("trying to add new user");
            if (user == null)
            {
                _logger.Error("user is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (var orphanageDBC = new OrphanageDbCNoBinary())
            {
                using (var Dbt = orphanageDBC.Database.BeginTransaction())
                {
                    int ret = 0;
                    if (user.Name != null)
                    {
                        var nameId = await _regularDataService.AddName(user.Name, orphanageDBC);

                        if (nameId > 0)
                        {
                            user.NameId = nameId;
                        }
                        else
                        {
                            Dbt.Rollback();
                            _logger.Warning($"Name object has not been added, nothing will be added, null will be returned");
                            return(null);
                        }
                    }
                    if (user.Address != null)
                    {
                        var addressId = await _regularDataService.AddAddress(user.Address, orphanageDBC);

                        if (addressId == -1)
                        {
                            Dbt.Rollback();
                            _logger.Warning($"Address object has not been added, nothing will be added, null will be returned");
                            return(null);
                        }
                        user.AddressId = addressId;
                    }
                    user.Accounts   = null;
                    user.Bails      = null;
                    user.Caregivers = null;
                    user.Famlies    = null;
                    user.Fathers    = null;
                    user.Guarantors = null;
                    user.Mothers    = null;
                    user.Orphans    = null;
                    _logger.Information("trying to hash the user password");
                    user.Password = _passwordHasher.Hash(user.Password);
                    orphanageDBC.Users.Add(user);
                    ret = await orphanageDBC.SaveChangesAsync();

                    if (ret >= 1)
                    {
                        Dbt.Commit();
                        _logger.Information($"new user object with id {user.Id} has been added");
                        _logger.Information($"the caregiver object with id {user.Id}  will be returned");
                        return(user);
                    }
                    else
                    {
                        Dbt.Rollback();
                        _logger.Warning($"something went wrong, nothing was added, null will be returned");
                        return(null);
                    }
                }
            }
        }
예제 #6
0
        public void BlockUserSelfLoop(ref OrphanageDataModel.Persons.User user)
        {
            if (user == null)
            {
                return;
            }

            dynamic userN = user.Name;

            BlockForignKeys(ref userN);
            user.Name = userN;

            dynamic userA = user.Address;

            BlockForignKeys(ref userA);
            user.Address = userA;

            if (user.Accounts != null)
            {
                foreach (var acc in user.Accounts)
                {
                    acc.ActingUser = null;
                }
            }
            if (user.Bails != null)
            {
                foreach (var bail in user.Bails)
                {
                    bail.ActingUser = null;
                }
            }
            if (user.Caregivers != null)
            {
                foreach (var careg in user.Caregivers)
                {
                    careg.ActingUser = null;
                }
            }
            if (user.Famlies != null)
            {
                foreach (var fam in user.Famlies)
                {
                    fam.ActingUser = null;
                }
            }
            if (user.Fathers != null)
            {
                foreach (var fath in user.Fathers)
                {
                    fath.ActingUser = null;
                }
            }
            if (user.Guarantors != null)
            {
                foreach (var gu in user.Fathers)
                {
                    gu.ActingUser = null;
                }
            }
            if (user.Mothers != null)
            {
                foreach (var mo in user.Mothers)
                {
                    mo.ActingUser = null;
                }
            }
            if (user.Orphans != null)
            {
                foreach (var orp in user.Orphans)
                {
                    orp.ActingUser = null;
                }
            }
        }