public async Task <IActionResult> Register([FromBody] RegisterModel model) { if (TenjinUtils.IsStringEmpty(model?.Username) || TenjinUtils.IsStringEmpty(model?.Password) || TenjinUtils.IsStringEmpty(model?.Code)) { return(BadRequest()); } if (model.Code.IsObjectIdEmpty()) { return(BadRequest()); } var linked = await _service.IsLinked(model.Code); var existed = await _service.IsExisted(model.Username); if (existed || linked) { return(BadRequest()); } await _service.Add(new User { Code = model.Code.ToObjectId(), Name = model.Name.NormalizeString(), Username = model.Username.NormalizeString(), Password = model.Password, Permission = model.Permission }); return(Ok()); }
public AuthenticationTicket Decode(string token) { try { if (TenjinUtils.IsStringEmpty(token)) { return(default);
public FilterDefinition <T> GetDefinition <T>() { if (_category == "$in") { var value = _token.Value <JArray>("v"); var elements = value?.Values <JValue>()?.Select(x => x.Value)?.Where(IsValuable)?.ToList(); return(TenjinUtils.IsArrayNotEmpty(elements) ? Builders <T> .Filter.In(_name, elements) : default);
private string GetAddress(ActionExecutingContext context) { var address = GetHeader(context, "TENJIN-CLIENT-IP"); if (TenjinUtils.IsStringNotEmpty(address)) { return(address); } address = GetHeader(context, "CF-Connecting-IP"); if (TenjinUtils.IsStringNotEmpty(address)) { return(address); } address = GetHeader(context, "X-Forwarded-For"); if (TenjinUtils.IsStringNotEmpty(address)) { return(address); } address = context.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); if (TenjinUtils.IsStringNotEmpty(address)) { return(address); } return(string.Empty); }
public override async Task Add(DocumentImport entity) { await base.Add(entity); if (TenjinUtils.IsArrayNotEmpty(entity.Infos)) { foreach (var info in entity.Infos) { var filter = Builders <WarehouseInventory> .Filter.And( Builders <WarehouseInventory> .Filter.Eq(x => x.MaterialCode, info.Code), Builders <WarehouseInventory> .Filter.Eq(x => x.WarehouseCode, entity.WarehouseFromCode), Builders <WarehouseInventory> .Filter.Eq(x => x.UnitCode, info.UnitCode)); var updater = Builders <WarehouseInventory> .Update .Inc(x => x.Inventory, info.Quantity) .Set(x => x.UnitCode, info.UnitCode) .Set(x => x.WarehouseCode, entity.WarehouseFromCode) .Set(x => x.MaterialCode, info.Code) .Set(x => x.MaterialGroupCode, info.MaterialGroupCode) .Set(x => x.MaterialSubGroupCode, info.MaterialSubGroupCode) .Set(x => x.MaterialGroupTypeCode, info.MaterialGroupTypeCode) .Set(x => x.SerialNumber, info.SerialNumber) .Set(x => x.IsPublished, true) .Set(x => x.Specification, info.Specification); await _context.WarehouseInventoryRepository.UpsertOne(filter, updater); } } }
public async Task <bool> IsExisted(string username) { if (TenjinUtils.IsStringEmpty(username)) { throw new Exception($"Username is required."); } username = username.NormalizeString().ToLower(); return(await Count(x => username.Equals(x.Username)) > 0); }
public async Task <User> GetByUsername(string username) { if (TenjinUtils.IsStringEmpty(username)) { throw new Exception($"Username is required."); } username = username.NormalizeString().ToLower(); return(await GetSingleByExpression(x => username.Equals(x.Username))); }
public async Task <User> Login(string username, string password) { if (TenjinUtils.IsStringEmpty(password)) { throw new Exception($"Password is required."); } username = username.NormalizeString().ToLower(); var user = await GetByUsername(username); return(TenjinUtils.IsStringEmpty(user?.Password) ? default : password.Equals(user.Password) || password.VerifyRijndaelHash(user.Password) ? user : default);
private static string GetBearerToken(ActionContext context) { if (!context.HttpContext.Request.Headers.TryGetValue(TenjinConstants.AUTHORIZATION_HEADER_NAME, out var values)) { return(string.Empty); } var header = values.FirstOrDefault(); return(TenjinUtils.IsStringNotEmpty(header) && header.StartsWith(TenjinConstants.BEARER_AUTHORIZATION_NAME, StringComparison.OrdinalIgnoreCase) ? header : string.Empty); }
public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordModel model) { if (TenjinUtils.IsStringEmpty(model?.Code) || TenjinUtils.IsStringEmpty(model?.NewPassword) || model.NewPassword != model.ConfirmPassword) { return(BadRequest()); } var user = await _service.GetSingleByExpression(x => x.Code == model.Code.ToObjectId()); if (user == null) { return(BadRequest()); } await _service.ChangePassword(user.Id, model.NewPassword); return(Ok()); }
public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordModel model) { if (TenjinUtils.IsStringEmpty(AUTH_CODE) || TenjinUtils.IsStringEmpty(model?.OldPassword) || TenjinUtils.IsStringEmpty(model?.NewPassword) || model.NewPassword != model.ConfirmPassword) { return(BadRequest()); } var user = await _service.GetByCode(AUTH_CODE); if (!model.OldPassword.Equals(user.Password) && !model.OldPassword.VerifyRijndaelHash(user.Password)) { return(BadRequest()); } await _service.ChangePassword(user.Id, model.NewPassword); return(Ok()); }
public FilterDefinition <T> GetPreExpression <T>() where T : BaseEntity { var expression = Builders <T> .Filter.Where(x => true); if (TenjinUtils.IsStringNotEmpty(_keyword)) { if (_keyword.Contains("*") && TenjinRefUtils.GenerateTextIndexes <T>(out var _)) { expression = Builders <T> .Filter.And(expression, Builders <T> .Filter.Text(_keyword.Replace("*", " ").Trim())); } else { var patterns = Regex.Matches(_keyword, "([a-zA-Z0-9]*)") .Select(x => x.Value?.Trim()).Where(x => !string.IsNullOrEmpty(x)) .Select(x => Builders <T> .Filter.Regex("ValueToSearch", new BsonRegularExpression(x))); expression = Builders <T> .Filter.And(expression, Builders <T> .Filter.Or(patterns)); } } return(Builders <T> .Filter.And(expression.Concat(_definitions.Where(x => x.GetStage() == HttpExpressionDefinition.PRE_STAGE).Select(x => x.GetDefinition <T>())))); }
public override async Task Add(DocumentExport entity) { entity.IsPublished = true; await base.Add(entity); if (TenjinUtils.IsArrayNotEmpty(entity.Infos)) { foreach (var info in entity.Infos) { var filter = Builders <WarehouseInventory> .Filter.And( Builders <WarehouseInventory> .Filter.Eq(x => x.MaterialCode, info.Code), Builders <WarehouseInventory> .Filter.Eq(x => x.WarehouseCode, entity.WarehouseFromCode), Builders <WarehouseInventory> .Filter.Eq(x => x.UnitCode, info.UnitCode)); var updater = Builders <WarehouseInventory> .Update .Inc(x => x.Inventory, -info.Quantity); await _context.WarehouseInventoryRepository.UpsertOne(filter, updater); } } }
public async Task <IActionResult> Post([FromBody] LoginModel model) { if (TenjinUtils.IsStringEmpty(model?.Username) || TenjinUtils.IsStringEmpty(model?.Password)) { await Logging("AUTH-TOKEN", model?.Username, "EMPTY"); return(BadRequest()); } var user = await _service.Login(model.Username, model.Password); if (user == null) { await Logging("AUTH-TOKEN", model?.Username, "NOT-FOUND"); return(BadRequest()); } var response = await _token.GetTokenResponse(user); await Logging("AUTH-TOKEN", model?.Username, "COMPLETED"); return(Ok(response)); }
public async Task <IActionResult> Renew() { if (!await _token.IsClientCorrect(User)) { await Logging("AUTH-RENEW", AUTH_USERNAME, "CLIENT-INCORRECT"); return(Unauthorized()); } var token = GetHeader(TenjinConstants.AUTHORIZATION_HEADER_NAME); if (TenjinUtils.IsStringEmpty(token) || TenjinUtils.IsStringEmpty(AUTH_CODE)) { await Logging("AUTH-RENEW", AUTH_USERNAME, "TOKEN-EMPTY"); return(Unauthorized()); } var ticket = OAuthBearerUtils.Instance.Decode(token); if (ticket?.Principal == null || ticket?.Properties?.ExpiresUtc < DateTime.UtcNow) { await Logging("AUTH-RENEW", AUTH_USERNAME, "EXPIRED"); return(Unauthorized()); } var user = await _service.GetByCode(AUTH_CODE); if (user == null) { await Logging("AUTH-RENEW", AUTH_USERNAME, "NOT-FOUND"); return(BadRequest()); } var response = await _token.GetTokenResponse(user); await Logging("AUTH-RENEW", AUTH_USERNAME, "COMPLETED"); return(Ok(response)); }