Exemple #1
0
        public IActionResult Me()
        {
            try
            {
                var Claims = User.Claims.ToList();
                Dictionary <string, object> data = new Dictionary <string, object>();
                data.Add("username", Claims.Find(c => c.Type.Equals("username")).Value);
                data.Add("profile", JsonConvert.DeserializeObject(Claims.Find(c => c.Type.Equals("profile")).Value));
                data.Add("permission", JsonConvert.DeserializeObject(Claims.Find(c => c.Type.Equals("permission")).Value));
                List <object> storeViews = new List <object>();
                foreach (var c in Claims.Where(c => c.Type.Equals("stores")).ToList())
                {
                    storeViews.Add(JsonConvert.DeserializeObject(c.Value));
                }
                data.Add("stores", storeViews);
                data.Add("iat", Claims.Find(c => c.Type.Equals("iat")).Value);

                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.OK_STATUS_CODE, General.OK_MESSAGE)
                    .Ok();

                Result.Add("data", data);
                return(Ok(Result));
            }
            catch (Exception e)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message)
                    .Fail();
                return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result));
            }
        }
Exemple #2
0
        public virtual async Task <IActionResult> GetDataByStrikeOff([FromRoute] int strikeOffId)
        {
            try
            {
                var data = await Facade.GetDataByStrikeOff(strikeOffId);

                if (data.Item1 == null)
                {
                    var objectData = new
                    {
                        Data    = data.Item1,
                        OrderNo = data.Item2
                    };
                    Dictionary <string, object> Result =
                        new ResultFormatter(ApiVersion, General.OK_STATUS_CODE, General.OK_MESSAGE)
                        .Ok();
                    Result.Add("data", objectData);
                    return(Ok(Result));
                }
                else
                {
                    DyestuffChemicalUsageReceiptViewModel viewModel = Mapper.Map <DyestuffChemicalUsageReceiptViewModel>(data.Item1);
                    var objectData = new
                    {
                        Data    = viewModel,
                        OrderNo = data.Item2
                    };
                    Dictionary <string, object> Result =
                        new ResultFormatter(ApiVersion, General.OK_STATUS_CODE, General.OK_MESSAGE)
                        .Ok();

                    Result.Add("data", objectData);
                    return(Ok(Result));
                }
            }
            catch (Exception e)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message)
                    .Fail();
                return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result));
            }
        }
        public async Task <IActionResult> GetAccountByDivisionName([FromRoute] string divisionName)
        {
            try
            {
                var users = await Service.GetAccountByDivisionName(divisionName);

                var userViewModels = Mapper.Map <List <AccountViewModel> >(users);

                var result = new ResultFormatter(ApiVersion, General.OK_STATUS_CODE, General.OK_MESSAGE).Ok();

                result.Add("data", userViewModels);
                return(Ok(result));
            }
            catch (Exception e)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message)
                    .Fail();
                return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result));
            }
        }
        public async Task <IActionResult> Post([FromBody] LoginViewModel User)
        {
            try
            {
                var account = await _accountService.Authenticate(User.Username, User.Password);

                if (account == null)
                {
                    Dictionary <string, object> Result =
                        new ResultFormatter(ApiVersion, General.NOT_FOUND_STATUS_CODE, General.NOT_FOUND_MESSAGE)
                        .Fail();
                    return(NotFound(Result));
                }
                else
                {
                    AccountViewModel viewModel = Mapper.Map <AccountViewModel>(account);

                    SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Secret));
                    var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

                    var header = new JwtHeader(credentials);

                    var payload = new JwtPayload
                    {
                        { "username", viewModel.username }
                    };

                    payload["profile"] = new {
                        viewModel.profile.firstname,
                        viewModel.profile.lastname,
                        viewModel.profile.gender,
                        viewModel.profile.dob,
                        viewModel.profile.email
                    };

                    string jsonRes = "{";

                    foreach (var item in viewModel.roles.SelectMany(x => x.permissions).GroupBy(x => x.unit.Code).Select(g => g.First()))
                    {
                        jsonRes = jsonRes + "'" + item.unit.Code + "'" + " : " + item.permission + ",";
                    }
                    jsonRes = jsonRes.Remove(jsonRes.Length - 1) + "}";

                    var jsonObject = JObject.Parse(jsonRes);

                    payload["permission"] = jsonObject;

                    payload["iat"] = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

                    var secToken = new JwtSecurityToken(header, payload);
                    var handler  = new JwtSecurityTokenHandler();

                    var tokenString = handler.WriteToken(secToken);

                    Dictionary <string, object> Result =
                        new ResultFormatter(ApiVersion, General.OK_STATUS_CODE, General.OK_MESSAGE)
                        .Ok();

                    Result.Add("data", tokenString);

                    return(Ok(Result));
                }
            }
            catch (Exception ex)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.BAD_REQUEST_STATUS_CODE, ex.Message)
                    .Fail();

                return(BadRequest(Result));
            }
        }