This commit is contained in:
@@ -216,7 +216,29 @@ namespace AyaNova.Api.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Duplicate User
|
||||
/// (Wiki and Attachments are not duplicated)
|
||||
/// </summary>
|
||||
/// <param name="id">Source object id</param>
|
||||
/// <param name="apiVersion">From route path</param>
|
||||
/// <returns>User</returns>
|
||||
[HttpPost("duplicate/{id}")]
|
||||
public async Task<IActionResult> DuplicateUser([FromRoute] long id, ApiVersion apiVersion)
|
||||
{
|
||||
if (!serverState.IsOpen)
|
||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||
UserBiz biz = UserBiz.GetBiz(ct, HttpContext);
|
||||
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
User o = await biz.DuplicateAsync(id);
|
||||
if (o == null)
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
else
|
||||
return CreatedAtAction(nameof(UserController.GetUser), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace AyaNova.Biz
|
||||
}
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
|
||||
|
||||
var HasLoginTechList = await ct.User.Where(z => z.Active == true && z.LastLogin != null && (
|
||||
z.UserType == UserType.Service ||
|
||||
z.UserType == UserType.ServiceContractor)).OrderByDescending(z => z.UserType).ThenBy(z => z.LastLogin).Take(CountOfTechsToSetInactive).ToListAsync();
|
||||
@@ -173,6 +173,41 @@ namespace AyaNova.Biz
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//DUPLICATE
|
||||
//
|
||||
internal async Task<User> DuplicateAsync(long id)
|
||||
{
|
||||
User dbObject = await ct.User.SingleOrDefaultAsync(z => z.Id == id);
|
||||
|
||||
if (dbObject == null)
|
||||
{
|
||||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
return null;
|
||||
}
|
||||
User newObject = new User();
|
||||
CopyObject.Copy(dbObject, newObject, "Id, Salt, Login, Password, CurrentAuthToken, DlKey, DlKeyExpire, Wiki, Serial");
|
||||
string newUniqueName = string.Empty;
|
||||
bool NotUnique = true;
|
||||
long l = 1;
|
||||
do
|
||||
{
|
||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||
NotUnique = await ct.User.AnyAsync(z => z.Name == newUniqueName);
|
||||
} while (NotUnique);
|
||||
newObject.Name = newUniqueName;
|
||||
newObject.Id = 0;
|
||||
newObject.Concurrency = 0;
|
||||
newObject.Salt = Hasher.GenerateSalt();
|
||||
newObject.Password = Hasher.GenerateSalt();
|
||||
newObject.Login = Hasher.GenerateSalt();
|
||||
await ct.User.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||
await SearchIndexAsync(newObject, true);
|
||||
await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||
return newObject;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// GET
|
||||
@@ -212,7 +247,7 @@ namespace AyaNova.Biz
|
||||
}
|
||||
User SnapshotOfOriginalDBObj = new User();
|
||||
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||
CopyObject.Copy(putObject, dbObject, "Id, Salt, CurrentAuthToken, DlKey, DlKeyExpire");
|
||||
CopyObject.Copy(putObject, dbObject, "Id, Salt, CurrentAuthToken, LoginKey, DlKey, DlKeyExpire");
|
||||
dbObject.Tags = TagBiz.NormalizeTags(dbObject.Tags);
|
||||
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||
|
||||
@@ -264,72 +299,6 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// //put
|
||||
// internal async Task<User> PutAsync(User putObject)
|
||||
// {
|
||||
// User dbObject = await ct.User.SingleOrDefaultAsync(z => z.Id == putObject.Id);
|
||||
// if (dbObject == null)
|
||||
// {
|
||||
// AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// //Get a snapshot of the original db value object before changes
|
||||
// User SnapshotOfOriginalDBObj = new User();
|
||||
// CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||
|
||||
// //Update the db object with the PUT object values
|
||||
// CopyObject.Copy(putObject, dbObject, "Id, Salt, CurrentAuthToken, DlKey, DlKeyExpire");
|
||||
// dbObject.Tags = TagBiz.NormalizeTags(dbObject.Tags);
|
||||
// dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||||
|
||||
// //NOTE: It's valid to call this without intending to change login or password (null values)
|
||||
// //Is the user updating the password?
|
||||
// if (!string.IsNullOrWhiteSpace(putObject.Password) && SnapshotOfOriginalDBObj.Password != putObject.Password)
|
||||
// {
|
||||
// //YES password is being updated:
|
||||
// dbObject.Password = Hasher.hash(SnapshotOfOriginalDBObj.Salt, putObject.Password);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// //No, use the snapshot password value
|
||||
// dbObject.Password = SnapshotOfOriginalDBObj.Password;
|
||||
// dbObject.Salt = SnapshotOfOriginalDBObj.Salt;
|
||||
// }
|
||||
// //Updating login?
|
||||
// if (!string.IsNullOrWhiteSpace(putObject.Login))
|
||||
// {
|
||||
// //YES Login is being updated:
|
||||
// dbObject.Login = putObject.Login;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// //No, use the original value
|
||||
// dbObject.Login = SnapshotOfOriginalDBObj.Login;
|
||||
// }
|
||||
|
||||
|
||||
// //Set "original" value of concurrency token to input token
|
||||
// //this will allow EF to check it out
|
||||
// ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||
|
||||
// await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||
// if (HasErrors)
|
||||
// return false;
|
||||
// await ct.SaveChangesAsync();
|
||||
|
||||
// //Log modification and save context
|
||||
// await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||
|
||||
// await SearchIndexAsync(dbObject, false);
|
||||
|
||||
// await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||||
|
||||
// return true;
|
||||
// }
|
||||
|
||||
/////////////////////////////////////////////
|
||||
//PASSWORD
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user