Users/DeleteOktaUser.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Okta.Core;
using Okta.Core.Models;
 
namespace Okta.Core.Automation
{
    [Cmdlet("Delete", "OktaUser", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
    public class DeleteOktaUser : OktaCmdlet
    {
        [Parameter(
            Mandatory = false,
            ValueFromPipelineByPropertyName = true,
            Position = 0,
            HelpMessage = "ID or username of the user to delete"
        )]
        public string Id { get; set; }
 
        [Parameter(
            Mandatory = false,
            ValueFromPipelineByPropertyName = true,
            Position = 1,
            HelpMessage = "Search query string (see http://developer.okta.com/docs/api/resources/users.html#find-users)"
        )]
        public string Query { get; set; }
 
        [Parameter(
            Mandatory = false,
            ValueFromPipelineByPropertyName = true,
            Position = 2,
            HelpMessage = "Use a filter (see http://developer.okta.com/docs/api/resources/users.html#list-users-with-a-filter)"
        )]
        public string Filter { get; set; }
 
        [Parameter(
            Mandatory = false,
            ValueFromPipelineByPropertyName = true,
            Position = 3,
            HelpMessage = "Use advanced search (see http://developer.okta.com/docs/api/resources/users.html#list-users-with-search)"
        )]
        public bool Search { get; set; }
 
        protected override void ProcessRecord()
        {
            var usersClient = Client.GetUsersClient();
            try
            {
                List<User> users = new List<User>();
                if (!string.IsNullOrEmpty(Id))
                {
                    var user = usersClient.Get(Id);
                    if (user != null)
                    {
                        users.Add(user);
                    }
                }
                else
                {
                    SearchType searchType = SearchType.Filter;
                    if (Search)
                    {
                        searchType = SearchType.ElasticSearch;
                    }
                    var usersEnum = usersClient.GetFilteredEnumerator(query: Query, searchType: searchType, filter: new FilterBuilder(Filter));
                    users = usersEnum.ToList<User>();
                }
 
                if (users.Count > 1)
                {
                    if (!ShouldProcess($"Delete-OktaUser matched {users.Count} users, and will permanently delete them if confirmed", $"This action will permanently delete {users.Count} users.", "Are you sure you want to perform this action?"))
                    {
                        WriteVerbose("Operation canceled, no users deleted.");
                        return;
                    }
                }
 
                foreach (User user in users)
                {
                    string strUserLogin = user.Profile.Login;
                    bool bDeactivated = false;
                    try
                    {
                        usersClient.Deactivate(user);
                        bDeactivated = true;
                    }
                    catch (OktaException oex2)
                    {
                        if (oex2.ErrorCode == OktaErrorCodes.ResourceNotFoundException)
                        {
                            bDeactivated = true;
                        }
                        else
                        {
                            throw; // Rethrow; will be handled by the outer try/catch
                        }
                    }
 
                    if (bDeactivated)
                    {
                        usersClient.Delete(user);
                        WriteObject(string.Format("Successfully deleted user: {0}", strUserLogin));
                    }
                }
            }
            catch (OktaException oex)
            {
                ErrorRecord er = new ErrorRecord(oex, oex.ErrorId, ErrorCategory.InvalidData, usersClient);
                ErrorDetails errorDetails = new ErrorDetails(string.Format("An error occurred: {0}", oex.ErrorSummary));
                er.ErrorDetails = errorDetails;
                WriteError(er);
            }
 
        }
    }
}